Debian Codegraph @

output/outelf.c

raw
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /* Copyright 1996-2025 The NASM Authors - All Rights Reserved */
3
4 /*
5 * Common code for outelf32 and outelf64
6 */
7
8 #include "compiler.h"
9
10
11 #include "nasm.h"
12 #include "nasmlib.h"
13 #include "error.h"
14 #include "saa.h"
15 #include "raa.h"
16 #include "stdscan.h"
17 #include "eval.h"
18 #include "outform.h"
19 #include "outlib.h"
20 #include "rbtree.h"
21 #include "hashtbl.h"
22 #include "ver.h"
23
24 #include "dwarf.h"
25 #include "stabs.h"
26 #include "outelf.h"
27 #include "elf.h"
28
29 #if defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32)
30
31 #define SECT_DELTA 32
32 static struct elf_section **sects;
33 static int nsects, sectlen;
34
35 #define SHSTR_DELTA 256
36 static char *shstrtab;
37 static int shstrtablen, shstrtabsize;
38
39 static struct SAA *syms;
40 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
41
42 static int32_t def_seg;
43
44 static struct RAA *bsym;
45
46 static struct SAA *symtab, *symtab_shndx;
47
48 static struct SAA *strs;
49 static uint32_t strslen;
50
51 static struct RAA *section_by_index;
52 static struct hash_table section_by_name;
53
54 static struct elf_symbol *fwds;
55
56 static char elf_module[FILENAME_MAX];
57 static char elf_dir[FILENAME_MAX];
58
59 extern const struct ofmt of_elf32;
60 extern const struct ofmt of_elf64;
61 extern const struct ofmt of_elfx32;
62
63 static struct ELF_SECTDATA {
64 void *data;
65 int64_t len;
66 bool is_saa;
67 } *elf_sects;
68
69 static int elf_nsect, nsections;
70 static int64_t elf_foffs;
71
72 static void elf_write(void);
73 static void elf_sect_write(struct elf_section *, const void *, size_t);
74 static void elf_sect_writeaddr(struct elf_section *, int64_t, size_t);
75 static void elf_section_header(int name, int type, uint64_t flags,
76 void *data, bool is_saa, uint64_t datalen,
77 int link, int info,
78 uint64_t align, uint64_t entsize);
79 static void elf_write_sections(void);
80 static size_t elf_build_symtab(void);
81 static int add_sectname(const char *, const char *);
82
83 /* First debugging section index */
84 static int sec_debug;
85
86 struct symlininfo {
87 int offset;
88 int section; /* index into sects[] */
89 int segto; /* internal section number */
90 char *name; /* shallow-copied pointer of section name */
91 };
92
93 struct linelist {
94 struct linelist *next;
95 struct linelist *last;
96 struct symlininfo info;
97 char *filename;
98 int line;
99 };
100
101 struct sectlist {
102 struct SAA *psaa;
103 int section;
104 int line;
105 int offset;
106 int file;
107 struct sectlist *next;
108 struct sectlist *last;
109 };
110
111 /* common debug variables */
112 static int currentline = 1;
113 static int debug_immcall = 0;
114
115 /* stabs debug variables */
116 static struct linelist *stabslines = 0;
117 static int numlinestabs = 0;
118 static char *stabs_filename = 0;
119 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
120 static int stablen, stabstrlen, stabrellen;
121
122 /* dwarf debug variables */
123 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
124 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
125 static int dwarf_numfiles = 0, dwarf_nsections;
126 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
127 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
128 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
129 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
130 abbrevlen, linelen, linerellen, framelen, loclen;
131 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
132
133 static struct elf_symbol *lastsym;
134
135 /* common debugging routines */
136 static void debug_typevalue(int32_t);
137
138 /* stabs debugging routines */
139 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t);
140 static void stabs_output(int, void *);
141 static void stabs_generate(void);
142 static void stabs_cleanup(void);
143
144 /* dwarf debugging routines */
145
146 /* This should match the order in elf_write() */
147 enum dwarf_sect {
148 DWARF_ARANGES,
149 DWARF_RELA_ARANGES,
150 DWARF_PUBNAMES,
151 DWARF_INFO,
152 DWARF_RELA_INFO,
153 DWARF_ABBREV,
154 DWARF_LINE,
155 DWARF_RELA_LINE,
156 DWARF_FRAME,
157 DWARF_LOC,
158 DWARF_NSECT
159 };
160
161 struct dwarf_format {
162 uint16_t dwarf_version;
163 uint16_t sect_version[DWARF_NSECT];
164 /* ... add more here to generalize further */
165 };
166 const struct dwarf_format *dwfmt;
167
168 static void dwarf32_init(void);
169 static void dwarfx32_init(void);
170 static void dwarf64_init(void);
171 static void dwarf_linenum(const char *filename, int32_t linenumber, int32_t);
172 static void dwarf_output(int, void *);
173 static void dwarf_generate(void);
174 static void dwarf_cleanup(void);
175 static void dwarf_findfile(const char *);
176 static void dwarf_findsect(const int);
177
178 struct elf_format_info {
179 size_t word; /* Word size (4 or 8) */
180 size_t ehdr_size; /* Size of the ELF header */
181 size_t shdr_size; /* Size of a section header */
182 size_t sym_size; /* Size of a symbol */
183 size_t relsize; /* Size of a reltype relocation */
184 char relpfx[8]; /* Relocation section prefix */
185 uint32_t reltype; /* Relocation section type */
186 uint16_t e_machine; /* Header e_machine field */
187 uint8_t ei_class; /* ELFCLASS32 or ELFCLASS64 */
188 bool elf64; /* 64-bit ELF */
189
190 /* Write a symbol */
191 void (*elf_sym)(const struct elf_symbol *);
192
193 /* Build a relocation table */
194 struct SAA *(*elf_build_reltab)(const struct elf_reloc *);
195 };
196 static const struct elf_format_info *efmt;
197
198 static void elf32_sym(const struct elf_symbol *sym);
199 static void elf64_sym(const struct elf_symbol *sym);
200
201 static struct SAA *elf32_build_reltab(const struct elf_reloc *r);
202 static struct SAA *elfx32_build_reltab(const struct elf_reloc *r);
203 static struct SAA *elf64_build_reltab(const struct elf_reloc *r);
204
205 static bool dfmt_is_stabs(void);
206 static bool dfmt_is_dwarf(void);
207
208 /*
209 * Special NASM section numbers which are used to define ELF special
210 * symbols.
211 */
212 static int32_t elf_gotpc_sect, elf_gotoff_sect;
213 static int32_t elf_got_sect, elf_plt_sect;
214 static int32_t elf_sym_sect, elf_gottpoff_sect, elf_tlsie_sect;
215
216 uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
217 uint8_t elf_abiver = 0; /* Current ABI version */
218
219 /* Known sections with nonstandard defaults. -n means n*pointer size. */
220 struct elf_known_section {
221 const char *name; /* Name of section */
222 int type; /* Section type (SHT_) */
223 uint32_t flags; /* Section flags (SHF_) */
224 int align; /* Section alignment */
225 int entsize; /* Entry size, if applicable */
226 };
227
228 static const struct elf_known_section elf_known_sections[] = {
229 { ".text", SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, 16, 0 },
230 { ".rodata", SHT_PROGBITS, SHF_ALLOC, 4, 0 },
231 { ".lrodata", SHT_PROGBITS, SHF_ALLOC, 4, 0 },
232 { ".data", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4, 0 },
233 { ".ldata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4, 0 },
234 { ".bss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4, 0 },
235 { ".lbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4, 0 },
236 { ".tdata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4, 0 },
237 { ".tbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4, 0 },
238 { ".comment", SHT_PROGBITS, 0, 1, 0 },
239 { ".preinit_array", SHT_PREINIT_ARRAY, SHF_ALLOC, -1, -1 },
240 { ".init_array", SHT_INIT_ARRAY, SHF_ALLOC, -1, -1 },
241 { ".fini_array", SHT_FINI_ARRAY, SHF_ALLOC, -1, -1 },
242 { ".note", SHT_NOTE, 0, 4, 0 },
243 { NULL /*default*/, SHT_PROGBITS, SHF_ALLOC, 1, 0 }
244 };
245
246 struct size_unit {
247 char name[8];
248 int bytes;
249 int align;
250 };
251 static const struct size_unit size_units[] =
252 {
253 { "byte", 1, 1 },
254 { "word", 2, 2 },
255 { "dword", 4, 4 },
256 { "qword", 8, 8 },
257 { "tword", 10, 2 },
258 { "tbyte", 10, 2 },
259 { "oword", 16, 16 },
260 { "xword", 16, 16 },
261 { "yword", 32, 32 },
262 { "zword", 64, 64 },
263 { "pointer", -1, -1 },
264 { "", 0, 0 }
265 };
266
267 static inline size_t to_bytes(int val)
268 {
269 return (val >= 0) ? (size_t)val : -val * efmt->word;
270 }
271
272 /* parse section attributes */
273 static void elf_section_attrib(char *name, char *attr, uint32_t *flags_and, uint32_t *flags_or,
274 uint64_t *alignp, uint64_t *entsize, int *type)
275 {
276 char *opt, *val, *next;
277 uint64_t align = 0;
278 uint64_t xalign = 0;
279
280 opt = nasm_skip_spaces(attr);
281 if (!opt || !*opt)
282 return;
283
284 while ((opt = nasm_opt_val(opt, &val, &next))) {
285 if (!nasm_stricmp(opt, "align")) {
286 if (!val) {
287 nasm_nonfatal("section align without value specified");
288 } else {
289 bool err;
290 uint64_t a = readnum(val, &err);
291 if (a && !is_power2(a)) {
292 nasm_error(ERR_NONFATAL,
293 "section alignment %"PRId64" is not a power of two",
294 a);
295 } else if (a > align) {
296 align = a;
297 }
298 }
299 } else if (!nasm_stricmp(opt, "alloc")) {
300 *flags_and |= SHF_ALLOC;
301 *flags_or |= SHF_ALLOC;
302 } else if (!nasm_stricmp(opt, "noalloc")) {
303 *flags_and |= SHF_ALLOC;
304 *flags_or &= ~SHF_ALLOC;
305 } else if (!nasm_stricmp(opt, "exec")) {
306 *flags_and |= SHF_EXECINSTR;
307 *flags_or |= SHF_EXECINSTR;
308 } else if (!nasm_stricmp(opt, "noexec")) {
309 *flags_and |= SHF_EXECINSTR;
310 *flags_or &= ~SHF_EXECINSTR;
311 } else if (!nasm_stricmp(opt, "write")) {
312 *flags_and |= SHF_WRITE;
313 *flags_or |= SHF_WRITE;
314 } else if (!nasm_stricmp(opt, "nowrite") ||
315 !nasm_stricmp(opt, "readonly")) {
316 *flags_and |= SHF_WRITE;
317 *flags_or &= ~SHF_WRITE;
318 } else if (!nasm_stricmp(opt, "tls")) {
319 *flags_and |= SHF_TLS;
320 *flags_or |= SHF_TLS;
321 } else if (!nasm_stricmp(opt, "notls")) {
322 *flags_and |= SHF_TLS;
323 *flags_or &= ~SHF_TLS;
324 } else if (!nasm_stricmp(opt, "merge")) {
325 *flags_and |= SHF_MERGE;
326 *flags_or |= SHF_MERGE;
327 } else if (!nasm_stricmp(opt, "nomerge")) {
328 *flags_and |= SHF_MERGE;
329 *flags_or &= ~SHF_MERGE;
330 } else if (!nasm_stricmp(opt, "strings")) {
331 *flags_and |= SHF_STRINGS;
332 *flags_or |= SHF_STRINGS;
333 } else if (!nasm_stricmp(opt, "nostrings")) {
334 *flags_and |= SHF_STRINGS;
335 *flags_or &= ~SHF_STRINGS;
336 } else if (!nasm_stricmp(opt, "progbits")) {
337 *type = SHT_PROGBITS;
338 } else if (!nasm_stricmp(opt, "nobits")) {
339 *type = SHT_NOBITS;
340 } else if (!nasm_stricmp(opt, "note")) {
341 *type = SHT_NOTE;
342 } else if (!nasm_stricmp(opt, "preinit_array")) {
343 *type = SHT_PREINIT_ARRAY;
344 } else if (!nasm_stricmp(opt, "init_array")) {
345 *type = SHT_INIT_ARRAY;
346 } else if (!nasm_stricmp(opt, "fini_array")) {
347 *type = SHT_FINI_ARRAY;
348 } else {
349 uint64_t mult;
350 size_t l;
351 const char *a = strchr(opt, '*');
352 bool err;
353 const struct size_unit *su;
354
355 if (a) {
356 l = a - opt - 1;
357 mult = readnum(a+1, &err);
358 } else {
359 l = strlen(opt);
360 mult = 1;
361 }
362
363 for (su = size_units; su->bytes; su++) {
364 if (!nasm_strnicmp(opt, su->name, l))
365 break;
366 }
367
368 if (su->bytes) {
369 *entsize = to_bytes(su->bytes) * mult;
370 xalign = to_bytes(su->align);
371 } else {
372 /* Unknown attribute */
373 nasm_warn(WARN_OTHER,
374 "unknown section attribute '%s' ignored on"
375 " declaration of section `%s'", opt, name);
376 }
377 }
378 opt = next;
379 }
380
381 switch (*type) {
382 case SHT_PREINIT_ARRAY:
383 case SHT_INIT_ARRAY:
384 case SHT_FINI_ARRAY:
385 if (!xalign)
386 xalign = efmt->word;
387 if (!*entsize)
388 *entsize = efmt->word;
389 break;
390 default:
391 break;
392 }
393
394 if (!align)
395 align = xalign;
396 if (!align)
397 align = SHA_ANY;
398
399 *alignp = align;
400 }
401
402 static enum directive_result
403 elf_directive(enum directive directive, char *value)
404 {
405 int64_t n;
406 bool err;
407 char *p;
408
409 switch (directive) {
410 case D_OSABI:
411 if (!value || !pass_first())
412 return DIRR_OK;
413
414 n = readnum(value, &err);
415 if (err) {
416 nasm_nonfatal("`osabi' directive requires a parameter");
417 return DIRR_ERROR;
418 }
419
420 if (n < 0 || n > 255) {
421 nasm_nonfatal("valid osabi numbers are 0 to 255");
422 return DIRR_ERROR;
423 }
424
425 elf_osabi = n;
426 elf_abiver = 0;
427
428 p = strchr(value,',');
429 if (!p)
430 return DIRR_OK;
431
432 n = readnum(p + 1, &err);
433 if (err || n < 0 || n > 255) {
434 nasm_nonfatal("invalid ABI version number (valid: 0 to 255)");
435 return DIRR_ERROR;
436 }
437
438 elf_abiver = n;
439 return DIRR_OK;
440
441 default:
442 return DIRR_UNKNOWN;
443 }
444 }
445
446 static void elf_init(void);
447
448 static void elf32_init(void)
449 {
450 static const struct elf_format_info ef_elf32 = {
451 4,
452 sizeof(Elf32_Ehdr),
453 sizeof(Elf32_Shdr),
454 sizeof(Elf32_Sym),
455 sizeof(Elf32_Rel),
456 ".rel",
457 SHT_REL,
458 EM_386,
459 ELFCLASS32,
460 false,
461
462 elf32_sym,
463 elf32_build_reltab
464 };
465 efmt = &ef_elf32;
466 elf_init();
467 }
468
469 static void elfx32_init(void)
470 {
471 static const struct elf_format_info ef_elfx32 = {
472 4,
473 sizeof(Elf32_Ehdr),
474 sizeof(Elf32_Shdr),
475 sizeof(Elf32_Sym),
476 sizeof(Elf32_Rela),
477 ".rela",
478 SHT_RELA,
479 EM_X86_64,
480 ELFCLASS32,
481 false,
482
483 elf32_sym,
484 elfx32_build_reltab
485 };
486 efmt = &ef_elfx32;
487 elf_init();
488 }
489
490 static void elf64_init(void)
491 {
492 static const struct elf_format_info ef_elf64 = {
493 8,
494 sizeof(Elf64_Ehdr),
495 sizeof(Elf64_Shdr),
496 sizeof(Elf64_Sym),
497 sizeof(Elf64_Rela),
498 ".rela",
499 SHT_RELA,
500 EM_X86_64,
501 ELFCLASS64,
502 true,
503
504 elf64_sym,
505 elf64_build_reltab
506 };
507 efmt = &ef_elf64;
508 elf_init();
509 }
510
511 static void elf_init(void)
512 {
513 static const char * const reserved_sections[] = {
514 ".shstrtab", ".strtab", ".symtab", ".symtab_shndx", NULL
515 };
516 const char * const *p;
517 const char * cur_path = nasm_realpath(inname);
518
519 strlcpy(elf_module, inname, sizeof(elf_module));
520 strlcpy(elf_dir, nasm_dirname(cur_path), sizeof(elf_dir));
521 sects = NULL;
522 nsects = sectlen = 0;
523 syms = saa_init((int32_t)sizeof(struct elf_symbol));
524 nlocals = nglobs = ndebugs = 0;
525 bsym = raa_init();
526 strs = saa_init(1L);
527 saa_wbytes(strs, "\0", 1L);
528 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
529 strslen = 2 + strlen(elf_module);
530 shstrtab = NULL;
531 shstrtablen = shstrtabsize = 0;
532 add_sectname("", ""); /* SHN_UNDEF */
533
534 fwds = NULL;
535
536 section_by_index = raa_init();
537
538 /*
539 * Add reserved section names to the section hash, with NULL
540 * as the data pointer
541 */
542 for (p = reserved_sections; *p; p++) {
543 struct hash_insert hi;
544 hash_find(&section_by_name, *p, &hi);
545 hash_add(&hi, *p, NULL);
546 }
547
548 /*
549 * FIXME: tlsie is Elf32 only and
550 * gottpoff is Elfx32|64 only.
551 */
552 elf_gotpc_sect = seg_alloc();
553 backend_label("..gotpc", elf_gotpc_sect + 1, 0L);
554 elf_gotoff_sect = seg_alloc();
555 backend_label("..gotoff", elf_gotoff_sect + 1, 0L);
556 elf_got_sect = seg_alloc();
557 backend_label("..got", elf_got_sect + 1, 0L);
558 elf_plt_sect = seg_alloc();
559 backend_label("..plt", elf_plt_sect + 1, 0L);
560 elf_sym_sect = seg_alloc();
561 backend_label("..sym", elf_sym_sect + 1, 0L);
562 elf_gottpoff_sect = seg_alloc();
563 backend_label("..gottpoff", elf_gottpoff_sect + 1, 0L);
564 elf_tlsie_sect = seg_alloc();
565 backend_label("..tlsie", elf_tlsie_sect + 1, 0L);
566
567 def_seg = seg_alloc();
568 }
569
570 static void elf_cleanup(void)
571 {
572 struct elf_reloc *r;
573 int i;
574
575 elf_write();
576 for (i = 0; i < nsects; i++) {
577 if (sects[i]->type != SHT_NOBITS)
578 saa_free(sects[i]->data);
579 if (sects[i]->rel)
580 saa_free(sects[i]->rel);
581 while (sects[i]->head) {
582 r = sects[i]->head;
583 sects[i]->head = sects[i]->head->next;
584 nasm_free(r);
585 }
586 }
587 hash_free(&section_by_name);
588 raa_free(section_by_index);
589 nasm_free(sects);
590 saa_free(syms);
591 raa_free(bsym);
592 saa_free(strs);
593 dfmt->cleanup();
594 }
595
596 /*
597 * Add entry to the elf .shstrtab section and increment nsections.
598 * Returns the section index for this new section.
599 *
600 * IMPORTANT: this needs to match the order the section headers are
601 * emitted.
602 */
603 static int add_sectname(const char *firsthalf, const char *secondhalf)
604 {
605 int l1 = strlen(firsthalf);
606 int l2 = strlen(secondhalf);
607
608 while (shstrtablen + l1 + l2 + 1 > shstrtabsize)
609 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
610
611 memcpy(shstrtab + shstrtablen, firsthalf, l1);
612 shstrtablen += l1;
613 memcpy(shstrtab + shstrtablen, secondhalf, l2+1);
614 shstrtablen += l2 + 1;
615
616 return nsections++;
617 }
618
619 static struct elf_section *
620 elf_make_section(char *name, int type, int flags, uint64_t align)
621 {
622 struct elf_section *s;
623
624 s = nasm_zalloc(sizeof(*s));
625
626 if (type != SHT_NOBITS)
627 s->data = saa_init(1L);
628 s->tail = &s->head;
629 if (!strcmp(name, ".text"))
630 s->index = def_seg;
631 else
632 s->index = seg_alloc();
633
634 s->name = nasm_strdup(name);
635 s->type = type;
636 s->flags = flags;
637 s->align = align;
638 s->shndx = add_sectname("", name);
639
640 if (nsects >= sectlen)
641 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
642 sects[nsects++] = s;
643
644 return s;
645 }
646
647 static int32_t elf_section_names(char *name, int *bits)
648 {
649 char *p;
650 uint32_t flags, flags_and, flags_or;
651 uint64_t align, entsize;
652 void **hp;
653 struct elf_section *s;
654 struct hash_insert hi;
655 int type;
656
657 if (!name) {
658 *bits = ofmt->maxbits;
659 return def_seg;
660 }
661
662 p = nasm_skip_word(name);
663 if (*p)
664 *p++ = '\0';
665 flags_and = flags_or = type = align = entsize = 0;
666
667 elf_section_attrib(name, p, &flags_and, &flags_or, &align, &entsize, &type);
668
669 hp = hash_find(&section_by_name, name, &hi);
670 if (hp) {
671 s = *hp;
672 if (!s) {
673 nasm_nonfatal("attempt to redefine reserved section name `%s'", name);
674 return NO_SEG;
675 }
676 } else {
677 const struct elf_known_section *ks = elf_known_sections;
678
679 while (ks->name) {
680 if (!strcmp(name, ks->name))
681 break;
682 ks++;
683 }
684
685 type = type ? type : ks->type;
686 if (!align)
687 align = to_bytes(ks->align);
688 if (!entsize)
689 entsize = to_bytes(ks->entsize);
690 flags = (ks->flags & ~flags_and) | flags_or;
691
692 s = elf_make_section(name, type, flags, align);
693 hash_add(&hi, s->name, s);
694 section_by_index = raa_write_ptr(section_by_index, s->index >> 1, s);
695 }
696
697 if ((type && s->type != type)
698 || ((s->flags & flags_and) != flags_or)
699 || (entsize && s->entsize && entsize != s->entsize)) {
700 nasm_warn(WARN_OTHER, "incompatible section attributes ignored on"
701 " redeclaration of section `%s'", name);
702 }
703
704 if (align > s->align)
705 s->align = align;
706
707 if (entsize && !s->entsize)
708 s->entsize = entsize;
709
710 if ((flags_or & SHF_MERGE) && s->entsize == 0) {
711 if (!(s->flags & SHF_STRINGS))
712 nasm_nonfatal("section attribute merge specified without an entry size or `strings'");
713 s->entsize = 1;
714 }
715
716 return s->index;
717 }
718
719 static inline bool sym_type_local(int type)
720 {
721 return ELF32_ST_BIND(type) == STB_LOCAL;
722 }
723
724 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
725 int is_global, char *special)
726 {
727 int pos = strslen;
728 struct elf_symbol *sym;
729 const char *spcword = nasm_skip_spaces(special);
730 int bind, type; /* st_info components */
731 const struct elf_section *sec = NULL;
732
733 nasm_debug(2, " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
734 name, segment, offset, is_global, special);
735
736 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
737 /*
738 * This is a NASM special symbol. We never allow it into
739 * the ELF symbol table, even if it's a valid one. If it
740 * _isn't_ a valid one, we should barf immediately.
741 *
742 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
743 */
744 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
745 strcmp(name, "..got") && strcmp(name, "..plt") &&
746 strcmp(name, "..sym") && strcmp(name, "..gottpoff") &&
747 strcmp(name, "..tlsie"))
748 nasm_nonfatal("unrecognised special symbol `%s'", name);
749 return;
750 }
751
752 if (is_global == 3) {
753 struct elf_symbol **s;
754 /*
755 * Fix up a forward-reference symbol size from the first
756 * pass.
757 */
758 for (s = &fwds; *s; s = &(*s)->nextfwd)
759 if (!strcmp((*s)->name, name)) {
760 struct tokenval tokval;
761 expr *e;
762 char *p = nasm_skip_spaces(nasm_skip_word(special));
763
764 stdscan_reset(p);
765 tokval.t_type = TOKEN_INVALID;
766 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
767 if (e) {
768 if (!is_simple(e))
769 nasm_nonfatal("cannot use relocatable"
770 " expression as symbol size");
771 else
772 (*s)->size = reloc_value(e);
773 }
774
775 /*
776 * Remove it from the list of unresolved sizes.
777 */
778 nasm_free((*s)->name);
779 *s = (*s)->nextfwd;
780 return;
781 }
782 return; /* it wasn't an important one */
783 }
784
785 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
786 strslen += 1 + strlen(name);
787
788 lastsym = sym = saa_wstruct(syms);
789
790 memset(&sym->symv, 0, sizeof(struct rbtree));
791
792 sym->strpos = pos;
793 bind = is_global ? STB_GLOBAL : STB_LOCAL;
794 type = STT_NOTYPE;
795 sym->other = STV_DEFAULT;
796 sym->size = 0;
797 if (segment == NO_SEG) {
798 sym->section = XSHN_ABS;
799 } else {
800 sym->section = XSHN_UNDEF;
801 if (segment == def_seg) {
802 /* we have to be sure at least text section is there */
803 int tempint;
804 if (segment != elf_section_names(".text", &tempint))
805 nasm_panic("strange segment conditions in ELF driver");
806 }
807 sec = raa_read_ptr(section_by_index, segment >> 1);
808 if (sec)
809 sym->section = sec->shndx;
810 }
811
812 if (is_global == 2) {
813 sym->size = offset;
814 sym->symv.key = 0;
815 sym->section = XSHN_COMMON;
816 /*
817 * We have a common variable. Check the special text to see
818 * if it's a valid number and power of two; if so, store it
819 * as the alignment for the common variable.
820 *
821 * XXX: this should allow an expression.
822 */
823 if (spcword) {
824 bool err;
825 sym->symv.key = readnum(spcword, &err);
826 if (err)
827 nasm_nonfatal("alignment constraint `%s' is not a"
828 " valid number", special);
829 else if (!is_power2(sym->symv.key))
830 nasm_nonfatal("alignment constraint `%s' is not a"
831 " power of two", special);
832 spcword = nasm_skip_spaces(nasm_skip_word(spcword));
833 }
834 } else {
835 sym->symv.key = (sym->section == XSHN_UNDEF ? 0 : offset);
836 }
837
838 if (spcword && *spcword) {
839 const char *wend;
840 bool ok = true;
841
842 while (ok) {
843 size_t wlen;
844 wend = nasm_skip_word(spcword);
845 wlen = wend - spcword;
846
847 switch (wlen) {
848 case 4:
849 if (!nasm_strnicmp(spcword, "data", wlen))
850 type = STT_OBJECT;
851 else if (!nasm_strnicmp(spcword, "weak", wlen))
852 bind = STB_WEAK;
853 else
854 ok = false;
855 break;
856
857 case 6:
858 if (!nasm_strnicmp(spcword, "notype", wlen))
859 type = STT_NOTYPE;
860 else if (!nasm_strnicmp(spcword, "object", wlen))
861 type = STT_OBJECT;
862 else if (!nasm_strnicmp(spcword, "hidden", wlen))
863 sym->other = STV_HIDDEN;
864 else if (!nasm_strnicmp(spcword, "strong", wlen))
865 bind = STB_GLOBAL;
866 else
867 ok = false;
868 break;
869
870 case 7:
871 if (!nasm_strnicmp(spcword, "default", wlen))
872 sym->other = STV_DEFAULT;
873 else
874 ok = false;
875 break;
876
877 case 8:
878 if (!nasm_strnicmp(spcword, "function", wlen))
879 type = STT_FUNC;
880 else if (!nasm_stricmp(spcword, "internal"))
881 sym->other = STV_INTERNAL;
882 else
883 ok = false;
884 break;
885
886 case 9:
887 if (!nasm_strnicmp(spcword, "protected", wlen))
888 sym->other = STV_PROTECTED;
889 else
890 ok = false;
891 break;
892
893 default:
894 ok = false;
895 break;
896 }
897
898 if (ok)
899 spcword = nasm_skip_spaces(wend);
900 }
901 if (!is_global && bind != STB_LOCAL) {
902 nasm_nonfatal("weak and strong only applies to global symbols");
903 bind = STB_LOCAL;
904 }
905
906 if (spcword && *spcword) {
907 struct tokenval tokval;
908 expr *e;
909 int fwd = 0;
910 char *saveme = stdscan_tell();
911
912 /*
913 * We have a size expression; attempt to
914 * evaluate it.
915 */
916 stdscan_reset((char *)spcword);
917 tokval.t_type = TOKEN_INVALID;
918 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
919 if (fwd) {
920 sym->nextfwd = fwds;
921 fwds = sym;
922 sym->name = nasm_strdup(name);
923 } else if (e) {
924 if (!is_simple(e))
925 nasm_nonfatal("cannot use relocatable"
926 " expression as symbol size");
927 else
928 sym->size = reloc_value(e);
929 }
930 stdscan_reset(saveme);
931 }
932 }
933
934 /*
935 * If it is in a TLS segment, mark symbol accordingly.
936 */
937 if (sec && (sec->flags & SHF_TLS))
938 type = STT_TLS;
939
940 /* Note: ELF32_ST_INFO() and ELF64_ST_INFO() are identical */
941 sym->type = ELF32_ST_INFO(bind, type);
942
943 if (sym_type_local(sym->type)) {
944 nlocals++;
945 } else {
946 /*
947 * If sym->section == SHN_ABS, then the first line of the
948 * else section would cause a core dump, because its a reference
949 * beyond the end of the section array.
950 * This behaviour is exhibited by this code:
951 * GLOBAL crash_nasm
952 * crash_nasm equ 0
953 * To avoid such a crash, such requests are silently discarded.
954 * This may not be the best solution.
955 */
956 if (sym->section == XSHN_UNDEF || sym->section == XSHN_COMMON) {
957 bsym = raa_write(bsym, segment, nglobs);
958 } else if (sym->section != XSHN_ABS) {
959 /*
960 * This is a global symbol; so we must add it to the rbtree
961 * of global symbols in its section.
962 *
963 * In addition, we check the special text for symbol
964 * type and size information.
965 */
966 sects[sym->section-1]->gsyms =
967 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
968
969 }
970 sym->globnum = nglobs;
971 nglobs++;
972 }
973 }
974
975 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
976 int64_t offset, int type)
977 {
978 struct elf_reloc *r;
979
980 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
981 sect->tail = &r->next;
982
983 r->address = sect->len;
984 r->offset = offset;
985
986 if (segment != NO_SEG) {
987 const struct elf_section *s;
988 s = raa_read_ptr(section_by_index, segment >> 1);
989 if (s)
990 r->symbol = s->shndx + 1;
991 else
992 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
993 }
994 r->type = type;
995
996 sect->nrelocs++;
997 }
998
999 /*
1000 * This routine deals with ..got and ..sym relocations: the more
1001 * complicated kinds. In shared-library writing, some relocations
1002 * with respect to global symbols must refer to the precise symbol
1003 * rather than referring to an offset from the base of the section
1004 * _containing_ the symbol. Such relocations call to this routine,
1005 * which searches the symbol list for the symbol in question.
1006 *
1007 * R_386_GOT32 | R_X86_64_GOT32 references require the _exact_ symbol address to be
1008 * used; R_386_32 | R_X86_64_32 references can be at an offset from the symbol.
1009 * The boolean argument `exact' tells us this.
1010 *
1011 * Return value is the adjusted value of `addr', having become an
1012 * offset from the symbol rather than the section. Should always be
1013 * zero when returning from an exact call.
1014 *
1015 * Limitation: if you define two symbols at the same place,
1016 * confusion will occur.
1017 *
1018 * Inefficiency: we search, currently, using a linked list which
1019 * isn't even necessarily sorted.
1020 */
1021 static int64_t elf_add_gsym_reloc(struct elf_section *sect,
1022 int32_t segment, uint64_t offset,
1023 int64_t pcrel, int type, bool exact)
1024 {
1025 struct elf_reloc *r;
1026 struct elf_section *s;
1027 struct elf_symbol *sym;
1028 struct rbtree *srb;
1029
1030 /*
1031 * First look up the segment/offset pair and find a global
1032 * symbol corresponding to it. If it's not one of our segments,
1033 * then it must be an external symbol, in which case we're fine
1034 * doing a normal elf_add_reloc after first sanity-checking
1035 * that the offset from the symbol is zero.
1036 */
1037 s = raa_read_ptr(section_by_index, segment >> 1);
1038 if (!s) {
1039 if (exact && offset)
1040 nasm_nonfatal("invalid access to an external symbol");
1041 else
1042 elf_add_reloc(sect, segment, offset - pcrel, type);
1043 return 0;
1044 }
1045
1046 srb = rb_search(s->gsyms, offset);
1047 if (!srb || (exact && srb->key != offset)) {
1048 nasm_nonfatal("unable to find a suitable global symbol"
1049 " for this reference");
1050 return 0;
1051 }
1052 sym = container_of(srb, struct elf_symbol, symv);
1053
1054 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
1055 sect->tail = &r->next;
1056
1057 r->next = NULL;
1058 r->address = sect->len;
1059 r->offset = offset - pcrel - sym->symv.key;
1060 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
1061 r->type = type;
1062
1063 sect->nrelocs++;
1064 return r->offset;
1065 }
1066
1067 static void elf32_out(const struct out_data *out)
1068 {
1069 OUT_LEGACY(out,segto,data,type,size,segment,wrt);
1070 struct elf_section *s;
1071 int64_t addr;
1072 int reltype, bytes;
1073 static struct symlininfo sinfo;
1074
1075 /*
1076 * handle absolute-assembly (structure definitions)
1077 */
1078 if (segto == NO_SEG) {
1079 if (type != OUT_RESERVE)
1080 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
1081 return;
1082 }
1083
1084 s = raa_read_ptr(section_by_index, segto >> 1);
1085 if (!s) {
1086 int tempint; /* ignored */
1087 if (segto != elf_section_names(".text", &tempint))
1088 nasm_panic("strange segment conditions in ELF driver");
1089 else
1090 s = sects[nsects - 1];
1091 }
1092
1093 /* again some stabs debugging stuff */
1094 sinfo.offset = s->len;
1095 /* Adjust to an index of the section table. */
1096 sinfo.section = s->shndx - 1;
1097 sinfo.segto = segto;
1098 sinfo.name = s->name;
1099 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1100 /* end of debugging stuff */
1101
1102 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1103 nasm_warn(WARN_OTHER, "attempt to initialize memory in"
1104 " BSS section `%s': ignored", s->name);
1105 s->len += realsize(type, size);
1106 return;
1107 }
1108
1109 switch (type) {
1110 case OUT_RESERVE:
1111 if (s->type != SHT_NOBITS) {
1112 nasm_warn(WARN_ZEROING, "uninitialized space declared in"
1113 " non-BSS section `%s': zeroing", s->name);
1114 elf_sect_write(s, NULL, size);
1115 } else
1116 s->len += size;
1117 break;
1118
1119 case OUT_RAWDATA:
1120 elf_sect_write(s, data, size);
1121 break;
1122
1123 case OUT_ADDRESS:
1124 {
1125 bool err = false;
1126 int asize = abs((int)size);
1127
1128 addr = *(int64_t *)data;
1129 if (segment != NO_SEG) {
1130 if (segment & 1) {
1131 nasm_nonfatal("ELF format does not support"
1132 " segment base references");
1133 } else {
1134 if (wrt == NO_SEG) {
1135 /*
1136 * The if() is a hack to deal with compilers which
1137 * don't handle switch() statements with 64-bit
1138 * expressions.
1139 */
1140 switch (asize) {
1141 case 1:
1142 elf_add_reloc(s, segment, 0, R_386_8);
1143 break;
1144 case 2:
1145 elf_add_reloc(s, segment, 0, R_386_16);
1146 break;
1147 case 4:
1148 elf_add_reloc(s, segment, 0, R_386_32);
1149 break;
1150 default: /* Error issued further down */
1151 err = true;
1152 break;
1153 }
1154 } else if (wrt == elf_gotpc_sect + 1) {
1155 /*
1156 * The user will supply GOT relative to $$. ELF
1157 * will let us have GOT relative to $. So we
1158 * need to fix up the data item by $-$$.
1159 */
1160 err = asize != 4;
1161 addr += s->len;
1162 elf_add_reloc(s, segment, 0, R_386_GOTPC);
1163 } else if (wrt == elf_gotoff_sect + 1) {
1164 err = asize != 4;
1165 elf_add_reloc(s, segment, 0, R_386_GOTOFF);
1166 } else if (wrt == elf_tlsie_sect + 1) {
1167 err = asize != 4;
1168 addr = elf_add_gsym_reloc(s, segment, addr, 0,
1169 R_386_TLS_IE, true);
1170 } else if (wrt == elf_got_sect + 1) {
1171 err = asize != 4;
1172 addr = elf_add_gsym_reloc(s, segment, addr, 0,
1173 R_386_GOT32, true);
1174 } else if (wrt == elf_sym_sect + 1) {
1175 switch (asize) {
1176 case 1:
1177 addr = elf_add_gsym_reloc(s, segment, addr, 0,
1178 R_386_8, false);
1179 break;
1180 case 2:
1181 addr = elf_add_gsym_reloc(s, segment, addr, 0,
1182 R_386_16, false);
1183 break;
1184 case 4:
1185 addr = elf_add_gsym_reloc(s, segment, addr, 0,
1186 R_386_32, false);
1187 break;
1188 default:
1189 err = true;
1190 break;
1191 }
1192 } else if (wrt == elf_plt_sect + 1) {
1193 nasm_nonfatal("ELF format cannot produce non-PC-"
1194 "relative PLT references");
1195 } else {
1196 nasm_nonfatal("ELF format does not support this"
1197 " use of WRT");
1198 wrt = NO_SEG; /* we can at least _try_ to continue */
1199 }
1200 }
1201 }
1202
1203 if (err) {
1204 nasm_nonfatal("Unsupported %d-bit ELF relocation", asize << 3);
1205 }
1206 elf_sect_writeaddr(s, addr, asize);
1207 break;
1208 }
1209
1210 case OUT_REL1ADR:
1211 reltype = R_386_PC8;
1212 bytes = 1;
1213 goto rel12adr;
1214 case OUT_REL2ADR:
1215 reltype = R_386_PC16;
1216 bytes = 2;
1217 goto rel12adr;
1218
1219 rel12adr:
1220 addr = *(int64_t *)data - size;
1221 nasm_assert(segment != segto);
1222 if (segment != NO_SEG && (segment & 1)) {
1223 nasm_nonfatal("ELF format does not support"
1224 " segment base references");
1225 } else {
1226 if (wrt == NO_SEG) {
1227 elf_add_reloc(s, segment, 0, reltype);
1228 } else {
1229 nasm_nonfatal("Unsupported %d-bit ELF relocation", bytes << 3);
1230 }
1231 }
1232 elf_sect_writeaddr(s, addr, bytes);
1233 break;
1234
1235 case OUT_REL4ADR:
1236 addr = *(int64_t *)data - size;
1237 if (segment == segto)
1238 nasm_panic("intra-segment OUT_REL4ADR");
1239 if (segment != NO_SEG && (segment & 1)) {
1240 nasm_nonfatal("ELF format does not support"
1241 " segment base references");
1242 } else {
1243 if (wrt == NO_SEG) {
1244 elf_add_reloc(s, segment, 0, R_386_PC32);
1245 } else if (wrt == elf_plt_sect + 1) {
1246 elf_add_reloc(s, segment, 0, R_386_PLT32);
1247 } else if (wrt == elf_gotpc_sect + 1 ||
1248 wrt == elf_gotoff_sect + 1 ||
1249 wrt == elf_got_sect + 1) {
1250 nasm_nonfatal("ELF format cannot produce PC-"
1251 "relative GOT references");
1252 } else {
1253 nasm_nonfatal("ELF format does not support this"
1254 " use of WRT");
1255 wrt = NO_SEG; /* we can at least _try_ to continue */
1256 }
1257 }
1258 elf_sect_writeaddr(s, addr, 4);
1259 break;
1260
1261 case OUT_REL8ADR:
1262 nasm_nonfatal("32-bit ELF format does not support 64-bit relocations");
1263 addr = 0;
1264 elf_sect_writeaddr(s, addr, 8);
1265 break;
1266
1267 default:
1268 panic();
1269 }
1270 }
1271
1272 static void elf64_out(const struct out_data *out)
1273 {
1274 OUT_LEGACY(out,segto,data,type,size,segment,wrt);
1275 struct elf_section *s;
1276 int64_t addr;
1277 int reltype, bytes;
1278 static struct symlininfo sinfo;
1279
1280 /*
1281 * handle absolute-assembly (structure definitions)
1282 */
1283 if (segto == NO_SEG) {
1284 if (type != OUT_RESERVE)
1285 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
1286 return;
1287 }
1288
1289 s = raa_read_ptr(section_by_index, segto >> 1);
1290 if (!s) {
1291 int tempint; /* ignored */
1292 if (segto != elf_section_names(".text", &tempint))
1293 nasm_panic("strange segment conditions in ELF driver");
1294 else
1295 s = sects[nsects - 1];
1296 }
1297
1298 /* again some stabs debugging stuff */
1299 sinfo.offset = s->len;
1300 /* Adjust to an index of the section table. */
1301 sinfo.section = s->shndx - 1;
1302 sinfo.segto = segto;
1303 sinfo.name = s->name;
1304 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1305 /* end of debugging stuff */
1306
1307 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1308 nasm_warn(WARN_OTHER, "attempt to initialize memory in"
1309 " BSS section `%s': ignored", s->name);
1310 s->len += realsize(type, size);
1311 return;
1312 }
1313
1314 switch (type) {
1315 case OUT_RESERVE:
1316 if (s->type != SHT_NOBITS) {
1317 nasm_warn(WARN_ZEROING, "uninitialized space declared in"
1318 " non-BSS section `%s': zeroing", s->name);
1319 elf_sect_write(s, NULL, size);
1320 } else
1321 s->len += size;
1322 break;
1323
1324 case OUT_RAWDATA:
1325 if (segment != NO_SEG)
1326 nasm_panic("OUT_RAWDATA with other than NO_SEG");
1327 elf_sect_write(s, data, size);
1328 break;
1329
1330 case OUT_ADDRESS:
1331 {
1332 int isize = (int)size;
1333 int asize = abs((int)size);
1334
1335 addr = *(int64_t *)data;
1336 if (segment == NO_SEG) {
1337 /* Do nothing */
1338 } else if (segment & 1) {
1339 nasm_nonfatal("ELF format does not support"
1340 " segment base references");
1341 } else {
1342 if (wrt == NO_SEG) {
1343 switch (isize) {
1344 case 1:
1345 case -1:
1346 elf_add_reloc(s, segment, addr, R_X86_64_8);
1347 break;
1348 case 2:
1349 case -2:
1350 elf_add_reloc(s, segment, addr, R_X86_64_16);
1351 break;
1352 case 4:
1353 elf_add_reloc(s, segment, addr, R_X86_64_32);
1354 break;
1355 case -4:
1356 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1357 break;
1358 case 8:
1359 case -8:
1360 elf_add_reloc(s, segment, addr, R_X86_64_64);
1361 break;
1362 default:
1363 nasm_panic("internal error elf64-hpa-871");
1364 break;
1365 }
1366 addr = 0;
1367 } else if (wrt == elf_gotpc_sect + 1) {
1368 /*
1369 * The user will supply GOT relative to $$. ELF
1370 * will let us have GOT relative to $. So we
1371 * need to fix up the data item by $-$$.
1372 */
1373 addr += s->len;
1374 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1375 addr = 0;
1376 } else if (wrt == elf_gotoff_sect + 1) {
1377 if (asize != 8) {
1378 nasm_nonfatal("ELF64 requires ..gotoff "
1379 "references to be qword");
1380 } else {
1381 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
1382 addr = 0;
1383 }
1384 } else if (wrt == elf_got_sect + 1) {
1385 switch (asize) {
1386 case 4:
1387 elf_add_gsym_reloc(s, segment, addr, 0,
1388 R_X86_64_GOT32, true);
1389 addr = 0;
1390 break;
1391 case 8:
1392 elf_add_gsym_reloc(s, segment, addr, 0,
1393 R_X86_64_GOT64, true);
1394 addr = 0;
1395 break;
1396 default:
1397 nasm_nonfatal("invalid ..got reference");
1398 break;
1399 }
1400 } else if (wrt == elf_sym_sect + 1) {
1401 switch (isize) {
1402 case 1:
1403 case -1:
1404 elf_add_gsym_reloc(s, segment, addr, 0,
1405 R_X86_64_8, false);
1406 addr = 0;
1407 break;
1408 case 2:
1409 case -2:
1410 elf_add_gsym_reloc(s, segment, addr, 0,
1411 R_X86_64_16, false);
1412 addr = 0;
1413 break;
1414 case 4:
1415 elf_add_gsym_reloc(s, segment, addr, 0,
1416 R_X86_64_32, false);
1417 addr = 0;
1418 break;
1419 case -4:
1420 elf_add_gsym_reloc(s, segment, addr, 0,
1421 R_X86_64_32S, false);
1422 addr = 0;
1423 break;
1424 case 8:
1425 case -8:
1426 elf_add_gsym_reloc(s, segment, addr, 0,
1427 R_X86_64_64, false);
1428 addr = 0;
1429 break;
1430 default:
1431 nasm_panic("internal error elf64-hpa-903");
1432 break;
1433 }
1434 } else if (wrt == elf_plt_sect + 1) {
1435 nasm_nonfatal("ELF format cannot produce non-PC-"
1436 "relative PLT references");
1437 } else {
1438 nasm_nonfatal("ELF format does not support this"
1439 " use of WRT");
1440 }
1441 }
1442 elf_sect_writeaddr(s, addr, asize);
1443 break;
1444 }
1445
1446 case OUT_REL1ADR:
1447 reltype = R_X86_64_PC8;
1448 bytes = 1;
1449 goto rel12adr;
1450
1451 case OUT_REL2ADR:
1452 reltype = R_X86_64_PC16;
1453 bytes = 2;
1454 goto rel12adr;
1455
1456 rel12adr:
1457 addr = *(int64_t *)data - size;
1458 if (segment == segto)
1459 nasm_panic("intra-segment OUT_REL1ADR");
1460 if (segment == NO_SEG) {
1461 /* Do nothing */
1462 } else if (segment & 1) {
1463 nasm_nonfatal("ELF format does not support"
1464 " segment base references");
1465 } else {
1466 if (wrt == NO_SEG) {
1467 elf_add_reloc(s, segment, addr, reltype);
1468 addr = 0;
1469 } else {
1470 nasm_nonfatal("Unsupported %d-bit ELF relocation", bytes << 3);
1471 }
1472 }
1473 elf_sect_writeaddr(s, addr, bytes);
1474 break;
1475
1476 case OUT_REL4ADR:
1477 addr = *(int64_t *)data - size;
1478 if (segment == segto)
1479 nasm_panic("intra-segment OUT_REL4ADR");
1480 if (segment == NO_SEG) {
1481 /* Do nothing */
1482 } else if (segment & 1) {
1483 nasm_nonfatal("ELF64 format does not support"
1484 " segment base references");
1485 } else {
1486 if (wrt == NO_SEG) {
1487 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1488 addr = 0;
1489 } else if (wrt == elf_plt_sect + 1) {
1490 elf_add_gsym_reloc(s, segment, addr+size, size,
1491 R_X86_64_PLT32, true);
1492 addr = 0;
1493 } else if (wrt == elf_gotpc_sect + 1 ||
1494 wrt == elf_got_sect + 1) {
1495 elf_add_gsym_reloc(s, segment, addr+size, size,
1496 R_X86_64_GOTPCREL, true);
1497 addr = 0;
1498 } else if (wrt == elf_gotoff_sect + 1 ||
1499 wrt == elf_got_sect + 1) {
1500 nasm_nonfatal("ELF64 requires ..gotoff references to be "
1501 "qword absolute");
1502 } else if (wrt == elf_gottpoff_sect + 1) {
1503 elf_add_gsym_reloc(s, segment, addr+size, size,
1504 R_X86_64_GOTTPOFF, true);
1505 addr = 0;
1506 } else {
1507 nasm_nonfatal("ELF64 format does not support this"
1508 " use of WRT");
1509 }
1510 }
1511 elf_sect_writeaddr(s, addr, 4);
1512 break;
1513
1514 case OUT_REL8ADR:
1515 addr = *(int64_t *)data - size;
1516 if (segment == segto)
1517 nasm_panic("intra-segment OUT_REL8ADR");
1518 if (segment == NO_SEG) {
1519 /* Do nothing */
1520 } else if (segment & 1) {
1521 nasm_nonfatal("ELF64 format does not support"
1522 " segment base references");
1523 } else {
1524 if (wrt == NO_SEG) {
1525 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1526 addr = 0;
1527 } else if (wrt == elf_gotpc_sect + 1 ||
1528 wrt == elf_got_sect + 1) {
1529 elf_add_gsym_reloc(s, segment, addr+size, size,
1530 R_X86_64_GOTPCREL64, true);
1531 addr = 0;
1532 } else if (wrt == elf_gotoff_sect + 1 ||
1533 wrt == elf_got_sect + 1) {
1534 nasm_nonfatal("ELF64 requires ..gotoff references to be "
1535 "absolute");
1536 } else if (wrt == elf_gottpoff_sect + 1) {
1537 nasm_nonfatal("ELF64 requires ..gottpoff references to be "
1538 "dword");
1539 } else {
1540 nasm_nonfatal("ELF64 format does not support this"
1541 " use of WRT");
1542 }
1543 }
1544 elf_sect_writeaddr(s, addr, 8);
1545 break;
1546
1547 default:
1548 panic();
1549 }
1550 }
1551
1552 static void elfx32_out(const struct out_data *out)
1553 {
1554 OUT_LEGACY(out,segto,data,type,size,segment,wrt);
1555 struct elf_section *s;
1556 int64_t addr;
1557 int reltype, bytes;
1558 static struct symlininfo sinfo;
1559
1560 /*
1561 * handle absolute-assembly (structure definitions)
1562 */
1563 if (segto == NO_SEG) {
1564 if (type != OUT_RESERVE)
1565 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
1566 return;
1567 }
1568
1569 s = raa_read_ptr(section_by_index, segto >> 1);
1570 if (!s) {
1571 int tempint; /* ignored */
1572 if (segto != elf_section_names(".text", &tempint))
1573 nasm_panic("strange segment conditions in ELF driver");
1574 else
1575 s = sects[nsects - 1];
1576 }
1577
1578 /* again some stabs debugging stuff */
1579 sinfo.offset = s->len;
1580 /* Adjust to an index of the section table. */
1581 sinfo.section = s->shndx - 1;
1582 sinfo.segto = segto;
1583 sinfo.name = s->name;
1584 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1585 /* end of debugging stuff */
1586
1587 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1588 nasm_warn(WARN_OTHER, "attempt to initialize memory in"
1589 " BSS section `%s': ignored", s->name);
1590 s->len += realsize(type, size);
1591 return;
1592 }
1593
1594 switch (type) {
1595 case OUT_RESERVE:
1596 if (s->type != SHT_NOBITS) {
1597 nasm_warn(WARN_ZEROING, "uninitialized space declared in"
1598 " non-BSS section `%s': zeroing", s->name);
1599 elf_sect_write(s, NULL, size);
1600 } else
1601 s->len += size;
1602 break;
1603
1604 case OUT_RAWDATA:
1605 if (segment != NO_SEG)
1606 nasm_panic("OUT_RAWDATA with other than NO_SEG");
1607 elf_sect_write(s, data, size);
1608 break;
1609
1610 case OUT_ADDRESS:
1611 {
1612 int isize = (int)size;
1613 int asize = abs((int)size);
1614
1615 addr = *(int64_t *)data;
1616 if (segment == NO_SEG) {
1617 /* Do nothing */
1618 } else if (segment & 1) {
1619 nasm_nonfatal("ELF format does not support"
1620 " segment base references");
1621 } else {
1622 if (wrt == NO_SEG) {
1623 switch (isize) {
1624 case 1:
1625 case -1:
1626 elf_add_reloc(s, segment, addr, R_X86_64_8);
1627 break;
1628 case 2:
1629 case -2:
1630 elf_add_reloc(s, segment, addr, R_X86_64_16);
1631 break;
1632 case 4:
1633 elf_add_reloc(s, segment, addr, R_X86_64_32);
1634 break;
1635 case -4:
1636 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1637 break;
1638 case 8:
1639 case -8:
1640 elf_add_reloc(s, segment, addr, R_X86_64_64);
1641 break;
1642 default:
1643 nasm_panic("internal error elfx32-hpa-871");
1644 break;
1645 }
1646 addr = 0;
1647 } else if (wrt == elf_gotpc_sect + 1) {
1648 /*
1649 * The user will supply GOT relative to $$. ELF
1650 * will let us have GOT relative to $. So we
1651 * need to fix up the data item by $-$$.
1652 */
1653 addr += s->len;
1654 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1655 addr = 0;
1656 } else if (wrt == elf_gotoff_sect + 1) {
1657 nasm_nonfatal("ELFX32 doesn't support "
1658 "R_X86_64_GOTOFF64");
1659 } else if (wrt == elf_got_sect + 1) {
1660 switch (asize) {
1661 case 4:
1662 elf_add_gsym_reloc(s, segment, addr, 0,
1663 R_X86_64_GOT32, true);
1664 addr = 0;
1665 break;
1666 default:
1667 nasm_nonfatal("invalid ..got reference");
1668 break;
1669 }
1670 } else if (wrt == elf_sym_sect + 1) {
1671 switch (isize) {
1672 case 1:
1673 case -1:
1674 elf_add_gsym_reloc(s, segment, addr, 0,
1675 R_X86_64_8, false);
1676 addr = 0;
1677 break;
1678 case 2:
1679 case -2:
1680 elf_add_gsym_reloc(s, segment, addr, 0,
1681 R_X86_64_16, false);
1682 addr = 0;
1683 break;
1684 case 4:
1685 elf_add_gsym_reloc(s, segment, addr, 0,
1686 R_X86_64_32, false);
1687 addr = 0;
1688 break;
1689 case -4:
1690 elf_add_gsym_reloc(s, segment, addr, 0,
1691 R_X86_64_32S, false);
1692 addr = 0;
1693 break;
1694 case 8:
1695 case -8:
1696 elf_add_gsym_reloc(s, segment, addr, 0,
1697 R_X86_64_64, false);
1698 addr = 0;
1699 break;
1700 default:
1701 nasm_panic("internal error elfx32-hpa-903");
1702 break;
1703 }
1704 } else if (wrt == elf_plt_sect + 1) {
1705 nasm_nonfatal("ELF format cannot produce non-PC-"
1706 "relative PLT references");
1707 } else {
1708 nasm_nonfatal("ELF format does not support this"
1709 " use of WRT");
1710 }
1711 }
1712 elf_sect_writeaddr(s, addr, asize);
1713 break;
1714 }
1715
1716 case OUT_REL1ADR:
1717 reltype = R_X86_64_PC8;
1718 bytes = 1;
1719 goto rel12adr;
1720
1721 case OUT_REL2ADR:
1722 reltype = R_X86_64_PC16;
1723 bytes = 2;
1724 goto rel12adr;
1725
1726 rel12adr:
1727 addr = *(int64_t *)data - size;
1728 if (segment == segto)
1729 nasm_panic("intra-segment OUT_REL1ADR");
1730 if (segment == NO_SEG) {
1731 /* Do nothing */
1732 } else if (segment & 1) {
1733 nasm_nonfatal("ELF format does not support"
1734 " segment base references");
1735 } else {
1736 if (wrt == NO_SEG) {
1737 elf_add_reloc(s, segment, addr, reltype);
1738 addr = 0;
1739 } else {
1740 nasm_nonfatal("unsupported %d-bit ELF relocation", bytes << 3);
1741 }
1742 }
1743 elf_sect_writeaddr(s, addr, bytes);
1744 break;
1745
1746 case OUT_REL4ADR:
1747 addr = *(int64_t *)data - size;
1748 if (segment == segto)
1749 nasm_panic("intra-segment OUT_REL4ADR");
1750 if (segment == NO_SEG) {
1751 /* Do nothing */
1752 } else if (segment & 1) {
1753 nasm_nonfatal("ELFX32 format does not support"
1754 " segment base references");
1755 } else {
1756 if (wrt == NO_SEG) {
1757 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1758 addr = 0;
1759 } else if (wrt == elf_plt_sect + 1) {
1760 elf_add_gsym_reloc(s, segment, addr+size, size,
1761 R_X86_64_PLT32, true);
1762 addr = 0;
1763 } else if (wrt == elf_gotpc_sect + 1 ||
1764 wrt == elf_got_sect + 1) {
1765 elf_add_gsym_reloc(s, segment, addr+size, size,
1766 R_X86_64_GOTPCREL, true);
1767 addr = 0;
1768 } else if (wrt == elf_gotoff_sect + 1 ||
1769 wrt == elf_got_sect + 1) {
1770 nasm_nonfatal("invalid ..gotoff reference");
1771 } else if (wrt == elf_gottpoff_sect + 1) {
1772 elf_add_gsym_reloc(s, segment, addr+size, size,
1773 R_X86_64_GOTTPOFF, true);
1774 addr = 0;
1775 } else {
1776 nasm_nonfatal("ELFX32 format does not support this use of WRT");
1777 }
1778 }
1779 elf_sect_writeaddr(s, addr, 4);
1780 break;
1781
1782 case OUT_REL8ADR:
1783 nasm_nonfatal("32-bit ELF format does not support 64-bit relocations");
1784 addr = 0;
1785 elf_sect_writeaddr(s, addr, 8);
1786 break;
1787
1788 default:
1789 panic();
1790 }
1791 }
1792
1793 /*
1794 * Section index/count with a specified overflow value (usually SHN_INDEX,
1795 * but 0 for e_shnum.
1796 */
1797 static inline uint16_t elf_shndx(int section, uint16_t overflow)
1798 {
1799 return htole16(section < (int)SHN_LORESERVE ? section : overflow);
1800 }
1801
1802 struct ehdr_common {
1803 uint8_t e_ident[EI_NIDENT];
1804 uint16_t e_type;
1805 uint16_t e_machine;
1806 uint32_t e_version;
1807 };
1808
1809 union ehdr {
1810 Elf32_Ehdr ehdr32;
1811 Elf64_Ehdr ehdr64;
1812 struct ehdr_common com;
1813 };
1814
1815 static void elf_write(void)
1816 {
1817 int align;
1818 char *p;
1819 int i;
1820 size_t symtablocal;
1821 int sec_shstrtab, sec_symtab, sec_strtab;
1822 union ehdr ehdr;
1823
1824 /*
1825 * Add any sections we don't already have:
1826 * rel/rela sections for the user sections, debug sections, and
1827 * the ELF special sections.
1828 */
1829
1830 sec_debug = nsections;
1831 if (dfmt_is_stabs()) {
1832 /* in case the debug information is wanted, just add these three sections... */
1833 add_sectname("", ".stab");
1834 add_sectname("", ".stabstr");
1835 add_sectname(efmt->relpfx, ".stab");
1836 } else if (dfmt_is_dwarf()) {
1837 /* the dwarf debug standard specifies the following ten sections,
1838 not all of which are currently implemented,
1839 although all of them are defined. */
1840 add_sectname("", ".debug_aranges");
1841 add_sectname(efmt->relpfx, ".debug_aranges");
1842 add_sectname("", ".debug_pubnames");
1843 add_sectname("", ".debug_info");
1844 add_sectname(efmt->relpfx, ".debug_info");
1845 add_sectname("", ".debug_abbrev");
1846 add_sectname("", ".debug_line");
1847 add_sectname(efmt->relpfx, ".debug_line");
1848 add_sectname("", ".debug_frame");
1849 add_sectname("", ".debug_loc");
1850 }
1851
1852 sec_shstrtab = add_sectname("", ".shstrtab");
1853 sec_symtab = add_sectname("", ".symtab");
1854 sec_strtab = add_sectname("", ".strtab");
1855
1856 /*
1857 * Build the symbol table and relocation tables.
1858 */
1859 symtablocal = elf_build_symtab();
1860
1861 /* Do we need an .symtab_shndx section? */
1862 if (symtab_shndx)
1863 add_sectname("", ".symtab_shndx");
1864
1865 for (i = 0; i < nsects; i++) {
1866 if (sects[i]->head) {
1867 add_sectname(efmt->relpfx, sects[i]->name);
1868 sects[i]->rel = efmt->elf_build_reltab(sects[i]->head);
1869 }
1870 }
1871
1872 /*
1873 * Output the ELF header.
1874 */
1875 nasm_zero(ehdr);
1876
1877 /* These fields are in the same place for 32 and 64 bits */
1878 memcpy(&ehdr.com.e_ident[EI_MAG0], ELFMAG, SELFMAG);
1879 ehdr.com.e_ident[EI_CLASS] = efmt->ei_class;
1880 ehdr.com.e_ident[EI_DATA] = ELFDATA2LSB;
1881 ehdr.com.e_ident[EI_VERSION] = EV_CURRENT;
1882 ehdr.com.e_ident[EI_OSABI] = elf_osabi;
1883 ehdr.com.e_ident[EI_ABIVERSION] = elf_abiver;
1884 ehdr.com.e_type = htole16(ET_REL);
1885 ehdr.com.e_machine = htole16(efmt->e_machine);
1886 ehdr.com.e_version = htole16(EV_CURRENT);
1887
1888 if (!efmt->elf64) {
1889 ehdr.ehdr32.e_shoff = htole32(sizeof ehdr);
1890 ehdr.ehdr32.e_ehsize = htole16(sizeof(Elf32_Ehdr));
1891 ehdr.ehdr32.e_shentsize = htole16(sizeof(Elf32_Shdr));
1892 ehdr.ehdr32.e_shnum = elf_shndx(nsections, 0);
1893 ehdr.ehdr32.e_shstrndx = elf_shndx(sec_shstrtab, SHN_XINDEX);
1894 } else {
1895 ehdr.ehdr64.e_shoff = htole64(sizeof ehdr);
1896 ehdr.ehdr64.e_ehsize = htole16(sizeof(Elf64_Ehdr));
1897 ehdr.ehdr64.e_shentsize = htole16(sizeof(Elf64_Shdr));
1898 ehdr.ehdr64.e_shnum = elf_shndx(nsections, 0);
1899 ehdr.ehdr64.e_shstrndx = elf_shndx(sec_shstrtab, SHN_XINDEX);
1900 }
1901
1902 nasm_write(&ehdr, sizeof(ehdr), ofile);
1903 elf_foffs = sizeof ehdr + efmt->shdr_size * nsections;
1904
1905 /*
1906 * Now output the section header table.
1907 */
1908 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1909 elf_foffs += align;
1910 elf_nsect = 0;
1911 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1912
1913 /* SHN_UNDEF */
1914 elf_section_header(0, SHT_NULL, 0, NULL, false,
1915 nsections > (int)SHN_LORESERVE ? nsections : 0,
1916 sec_shstrtab >= (int)SHN_LORESERVE ? sec_shstrtab : 0,
1917 0, 0, 0);
1918 p = shstrtab + 1;
1919
1920 /* The normal sections */
1921 for (i = 0; i < nsects; i++) {
1922 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1923 sects[i]->data, true,
1924 sects[i]->len, 0, 0,
1925 sects[i]->align, sects[i]->entsize);
1926 p += strlen(p) + 1;
1927 }
1928
1929 /* The debugging sections */
1930 if (dfmt_is_stabs()) {
1931 /* for debugging information, create the last three sections
1932 which are the .stab , .stabstr and .rel.stab sections respectively */
1933
1934 /* this function call creates the stab sections in memory */
1935 stabs_generate();
1936
1937 if (stabbuf && stabstrbuf && stabrelbuf) {
1938 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1939 stablen, sec_stabstr, 0, 4, 12);
1940 p += strlen(p) + 1;
1941
1942 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1943 stabstrlen, 0, 0, 4, 0);
1944 p += strlen(p) + 1;
1945
1946 /* link -> symtable info -> section to refer to */
1947 elf_section_header(p - shstrtab, efmt->reltype, 0,
1948 stabrelbuf, false, stabrellen,
1949 sec_symtab, sec_stab,
1950 efmt->word, efmt->relsize);
1951 p += strlen(p) + 1;
1952 }
1953 } else if (dfmt_is_dwarf()) {
1954 /* for dwarf debugging information, create the ten dwarf sections */
1955
1956 /* this function call creates the dwarf sections in memory */
1957 if (dwarf_fsect)
1958 dwarf_generate();
1959
1960 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1961 arangeslen, 0, 0, 1, 0);
1962 p += strlen(p) + 1;
1963
1964 elf_section_header(p - shstrtab, efmt->reltype, 0, arangesrelbuf, false,
1965 arangesrellen, sec_symtab,
1966 sec_debug_aranges,
1967 efmt->word, efmt->relsize);
1968 p += strlen(p) + 1;
1969
1970 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1971 false, pubnameslen, 0, 0, 1, 0);
1972 p += strlen(p) + 1;
1973
1974 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1975 infolen, 0, 0, 1, 0);
1976 p += strlen(p) + 1;
1977
1978 elf_section_header(p - shstrtab, efmt->reltype, 0, inforelbuf, false,
1979 inforellen, sec_symtab,
1980 sec_debug_info,
1981 efmt->word, efmt->relsize);
1982 p += strlen(p) + 1;
1983
1984 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1985 abbrevlen, 0, 0, 1, 0);
1986 p += strlen(p) + 1;
1987
1988 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1989 linelen, 0, 0, 1, 0);
1990 p += strlen(p) + 1;
1991
1992 elf_section_header(p - shstrtab, efmt->reltype, 0, linerelbuf, false,
1993 linerellen, sec_symtab,
1994 sec_debug_line,
1995 efmt->word, efmt->relsize);
1996 p += strlen(p) + 1;
1997
1998 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1999 framelen, 0, 0, 8, 0);
2000 p += strlen(p) + 1;
2001
2002 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
2003 loclen, 0, 0, 1, 0);
2004 p += strlen(p) + 1;
2005 }
2006
2007 /* .shstrtab */
2008 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
2009 shstrtablen, 0, 0, 1, 0);
2010 p += strlen(p) + 1;
2011
2012 /* .symtab */
2013 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
2014 symtab->datalen, sec_strtab, symtablocal,
2015 efmt->word, efmt->sym_size);
2016 p += strlen(p) + 1;
2017
2018 /* .strtab */
2019 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
2020 strslen, 0, 0, 1, 0);
2021 p += strlen(p) + 1
2022 ;
2023 /* .symtab_shndx */
2024 if (symtab_shndx) {
2025 elf_section_header(p - shstrtab, SHT_SYMTAB_SHNDX, 0,
2026 symtab_shndx, true, symtab_shndx->datalen,
2027 sec_symtab, 0, 1, 0);
2028 p += strlen(p) + 1;
2029 }
2030
2031 /* The relocation sections */
2032 for (i = 0; i < nsects; i++) {
2033 if (sects[i]->rel) {
2034 elf_section_header(p - shstrtab, efmt->reltype, 0,
2035 sects[i]->rel, true, sects[i]->rel->datalen,
2036 sec_symtab, sects[i]->shndx,
2037 efmt->word, efmt->relsize);
2038 p += strlen(p) + 1;
2039 }
2040 }
2041 fwritezero(align, ofile);
2042
2043 /*
2044 * Now output the sections.
2045 */
2046 elf_write_sections();
2047
2048 nasm_free(elf_sects);
2049 saa_free(symtab);
2050 if (symtab_shndx)
2051 saa_free(symtab_shndx);
2052 }
2053
2054 static size_t nsyms;
2055
2056 static void elf_sym(const struct elf_symbol *sym)
2057 {
2058 int shndx = sym->section;
2059
2060 /*
2061 * Careful here. This relies on sym->section being signed; for
2062 * special section indices this value needs to be cast to
2063 * (int16_t) so that it sign-extends, however, here SHN_LORESERVE
2064 * is used as an unsigned constant.
2065 */
2066 if (shndx >= (int)SHN_LORESERVE) {
2067 if (unlikely(!symtab_shndx)) {
2068 /* Create symtab_shndx and fill previous entries with zero */
2069 symtab_shndx = saa_init(1);
2070 saa_wbytes(symtab_shndx, NULL, nsyms << 2);
2071 }
2072 } else {
2073 shndx = 0; /* Section index table always write zero */
2074 }
2075
2076 if (symtab_shndx)
2077 saa_write32(symtab_shndx, shndx);
2078
2079 efmt->elf_sym(sym);
2080 nsyms++;
2081 }
2082
2083 static void elf32_sym(const struct elf_symbol *sym)
2084 {
2085 Elf32_Sym sym32;
2086
2087 sym32.st_name = htole32(sym->strpos);
2088 sym32.st_value = htole32(sym->symv.key);
2089 sym32.st_size = htole32(sym->size);
2090 sym32.st_info = sym->type;
2091 sym32.st_other = sym->other;
2092 sym32.st_shndx = elf_shndx(sym->section, SHN_XINDEX);
2093 saa_wbytes(symtab, &sym32, sizeof sym32);
2094 }
2095
2096 static void elf64_sym(const struct elf_symbol *sym)
2097 {
2098 Elf64_Sym sym64;
2099
2100 sym64.st_name = htole32(sym->strpos);
2101 sym64.st_value = htole64(sym->symv.key);
2102 sym64.st_size = htole64(sym->size);
2103 sym64.st_info = sym->type;
2104 sym64.st_other = sym->other;
2105 sym64.st_shndx = elf_shndx(sym->section, SHN_XINDEX);
2106 saa_wbytes(symtab, &sym64, sizeof sym64);
2107 }
2108
2109 static size_t elf_build_symtab(void)
2110 {
2111 struct elf_symbol *sym, xsym;
2112 size_t nlocal;
2113 int i;
2114
2115 symtab = saa_init(1);
2116 symtab_shndx = NULL;
2117
2118 /*
2119 * Zero symbol first as required by spec.
2120 */
2121 nasm_zero(xsym);
2122 elf_sym(&xsym);
2123
2124 /*
2125 * Next, an entry for the file name.
2126 */
2127 nasm_zero(xsym);
2128 xsym.strpos = 1;
2129 xsym.type = ELF32_ST_INFO(STB_LOCAL, STT_FILE);
2130 xsym.section = XSHN_ABS;
2131 elf_sym(&xsym);
2132
2133 /*
2134 * Now some standard symbols defining the segments, for relocation
2135 * purposes.
2136 */
2137 nasm_zero(xsym);
2138 for (i = 1; i <= nsects; i++) {
2139 xsym.type = ELF64_ST_INFO(STB_LOCAL, STT_SECTION);
2140 xsym.section = i;
2141 elf_sym(&xsym);
2142 }
2143
2144 /*
2145 * dwarf needs symbols for debug sections
2146 * which are relocation targets.
2147 */
2148 if (dfmt_is_dwarf()) {
2149 dwarf_infosym = nsyms;
2150 xsym.section = sec_debug_info;
2151 elf_sym(&xsym);
2152
2153 dwarf_abbrevsym = nsyms;
2154 xsym.section = sec_debug_abbrev;
2155 elf_sym(&xsym);
2156
2157 dwarf_linesym = nsyms;
2158 xsym.section = sec_debug_line;
2159 elf_sym(&xsym);
2160 }
2161
2162 /*
2163 * Now the other local symbols.
2164 */
2165 saa_rewind(syms);
2166 while ((sym = saa_rstruct(syms))) {
2167 if (!sym_type_local(sym->type))
2168 continue;
2169
2170 elf_sym(sym);
2171 }
2172
2173 nlocal = nsyms;
2174
2175 /*
2176 * Now the global symbols.
2177 */
2178 saa_rewind(syms);
2179 while ((sym = saa_rstruct(syms))) {
2180 if (sym_type_local(sym->type))
2181 continue;
2182
2183 elf_sym(sym);
2184 }
2185
2186 return nlocal;
2187 }
2188
2189 static struct SAA *elf32_build_reltab(const struct elf_reloc *r)
2190 {
2191 struct SAA *s;
2192 int32_t global_offset;
2193 Elf32_Rel rel32;
2194
2195 if (!r)
2196 return NULL;
2197
2198 s = saa_init(1L);
2199
2200 /*
2201 * How to onvert from a global placeholder to a real symbol index;
2202 * the +2 refers to the two special entries, the null entry and
2203 * the filename entry.
2204 */
2205 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
2206
2207 while (r) {
2208 int32_t sym = r->symbol;
2209
2210 if (sym >= GLOBAL_TEMP_BASE)
2211 sym += global_offset;
2212
2213 rel32.r_offset = htole32(r->address);
2214 rel32.r_info = htole32(ELF32_R_INFO(sym, r->type));
2215 saa_wbytes(s, &rel32, sizeof rel32);
2216
2217 r = r->next;
2218 }
2219
2220 return s;
2221 }
2222
2223 static struct SAA *elfx32_build_reltab(const struct elf_reloc *r)
2224 {
2225 struct SAA *s;
2226 int32_t global_offset;
2227 Elf32_Rela rela32;
2228
2229 if (!r)
2230 return NULL;
2231
2232 s = saa_init(1L);
2233
2234 /*
2235 * How to onvert from a global placeholder to a real symbol index;
2236 * the +2 refers to the two special entries, the null entry and
2237 * the filename entry.
2238 */
2239 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
2240
2241 while (r) {
2242 int32_t sym = r->symbol;
2243
2244 if (sym >= GLOBAL_TEMP_BASE)
2245 sym += global_offset;
2246
2247 rela32.r_offset = htole32(r->address);
2248 rela32.r_info = htole32(ELF32_R_INFO(sym, r->type));
2249 rela32.r_addend = htole32(r->offset);
2250 saa_wbytes(s, &rela32, sizeof rela32);
2251
2252 r = r->next;
2253 }
2254
2255 return s;
2256 }
2257
2258 static struct SAA *elf64_build_reltab(const struct elf_reloc *r)
2259 {
2260 struct SAA *s;
2261 int32_t global_offset;
2262 Elf64_Rela rela64;
2263
2264 if (!r)
2265 return NULL;
2266
2267 s = saa_init(1L);
2268
2269 /*
2270 * How to onvert from a global placeholder to a real symbol index;
2271 * the +2 refers to the two special entries, the null entry and
2272 * the filename entry.
2273 */
2274 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
2275
2276 while (r) {
2277 int32_t sym = r->symbol;
2278
2279 if (sym >= GLOBAL_TEMP_BASE)
2280 sym += global_offset;
2281
2282 rela64.r_offset = htole64(r->address);
2283 rela64.r_info = htole64(ELF64_R_INFO(sym, r->type));
2284 rela64.r_addend = htole64(r->offset);
2285 saa_wbytes(s, &rela64, sizeof rela64);
2286
2287 r = r->next;
2288 }
2289
2290 return s;
2291 }
2292
2293 static void elf_section_header(int name, int type, uint64_t flags,
2294 void *data, bool is_saa, uint64_t datalen,
2295 int link, int info,
2296 uint64_t align, uint64_t entsize)
2297 {
2298 elf_sects[elf_nsect].data = data;
2299 elf_sects[elf_nsect].len = datalen;
2300 elf_sects[elf_nsect].is_saa = is_saa;
2301 elf_nsect++;
2302
2303 if (!efmt->elf64) {
2304 Elf32_Shdr shdr;
2305
2306 shdr.sh_name = htole32(name);
2307 shdr.sh_type = htole32(type);
2308 shdr.sh_flags = htole32(flags);
2309 shdr.sh_addr = 0;
2310 shdr.sh_offset = htole32(type == SHT_NULL ? 0 : elf_foffs);
2311 shdr.sh_size = htole32(datalen);
2312 if (data)
2313 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2314 shdr.sh_link = htole32(link);
2315 shdr.sh_info = htole32(info);
2316 shdr.sh_addralign = htole32(align);
2317 shdr.sh_entsize = htole32(entsize);
2318
2319 nasm_write(&shdr, sizeof shdr, ofile);
2320 } else {
2321 Elf64_Shdr shdr;
2322
2323 shdr.sh_name = htole32(name);
2324 shdr.sh_type = htole32(type);
2325 shdr.sh_flags = htole64(flags);
2326 shdr.sh_addr = 0;
2327 shdr.sh_offset = htole64(type == SHT_NULL ? 0 : elf_foffs);
2328 shdr.sh_size = htole64(datalen);
2329 if (data)
2330 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2331 shdr.sh_link = htole32(link);
2332 shdr.sh_info = htole32(info);
2333 shdr.sh_addralign = htole64(align);
2334 shdr.sh_entsize = htole64(entsize);
2335
2336 nasm_write(&shdr, sizeof shdr, ofile);
2337 }
2338 }
2339
2340 static void elf_write_sections(void)
2341 {
2342 int i;
2343 for (i = 0; i < elf_nsect; i++)
2344 if (elf_sects[i].data) {
2345 int32_t len = elf_sects[i].len;
2346 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
2347 int32_t align = reallen - len;
2348 if (elf_sects[i].is_saa)
2349 saa_fpwrite(elf_sects[i].data, ofile);
2350 else
2351 nasm_write(elf_sects[i].data, len, ofile);
2352 fwritezero(align, ofile);
2353 }
2354 }
2355
2356 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
2357 {
2358 saa_wbytes(sect->data, data, len);
2359 sect->len += len;
2360 }
2361
2362 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
2363 {
2364 saa_writeaddr(sect->data, data, len);
2365 sect->len += len;
2366 }
2367
2368 static void elf_sectalign(int32_t seg, unsigned int value)
2369 {
2370 struct elf_section *s;
2371
2372 s = raa_read_ptr(section_by_index, seg >> 1);
2373 if (!s || !is_power2(value))
2374 return;
2375
2376 if (value > s->align)
2377 s->align = value;
2378 }
2379
2380 extern macros_t elf_stdmac[];
2381
2382 /* Claim "elf" as a pragma namespace, for the future */
2383 static const struct pragma_facility elf_pragma_list[] =
2384 {
2385 { "elf", NULL },
2386 { NULL, NULL } /* Implements the canonical output name */
2387 };
2388
2389
2390 static const struct dfmt elf32_df_dwarf = {
2391 "ELF32 (i386) dwarf (newer)",
2392 "dwarf",
2393 dwarf32_init,
2394 dwarf_linenum,
2395 null_debug_deflabel,
2396 NULL, /* .debug_smacros */
2397 NULL, /* .debug_include */
2398 NULL, /* .debug_mmacros */
2399 null_debug_directive,
2400 debug_typevalue,
2401 dwarf_output,
2402 dwarf_cleanup,
2403 NULL /* pragma list */
2404 };
2405
2406 static const struct dfmt elf32_df_stabs = {
2407 "ELF32 (i386) stabs (older)",
2408 "stabs",
2409 null_debug_init,
2410 stabs_linenum,
2411 null_debug_deflabel,
2412 NULL, /* .debug_smacros */
2413 NULL, /* .debug_include */
2414 NULL, /* .debug_mmacros */
2415 null_debug_directive,
2416 debug_typevalue,
2417 stabs_output,
2418 stabs_cleanup,
2419 NULL /* pragma list */
2420 };
2421
2422 static const struct dfmt * const elf32_debugs_arr[3] =
2423 { &elf32_df_dwarf, &elf32_df_stabs, NULL };
2424
2425 const struct ofmt of_elf32 = {
2426 "ELF32 (i386) (Linux, most Unix variants)",
2427 "elf32",
2428 ".o",
2429 0,
2430 32,
2431 elf32_debugs_arr,
2432 &elf32_df_dwarf,
2433 elf_stdmac,
2434 elf32_init,
2435 null_reset,
2436 elf32_out,
2437 elf_deflabel,
2438 elf_section_names,
2439 NULL,
2440 elf_sectalign,
2441 null_segbase,
2442 elf_directive,
2443 elf_cleanup,
2444 elf_pragma_list,
2445 };
2446
2447 static const struct dfmt elf64_df_dwarf = {
2448 "ELF64 (x86-64) dwarf (newer)",
2449 "dwarf",
2450 dwarf64_init,
2451 dwarf_linenum,
2452 null_debug_deflabel,
2453 NULL, /* .debug_smacros */
2454 NULL, /* .debug_include */
2455 NULL, /* .debug_mmacros */
2456 null_debug_directive,
2457 debug_typevalue,
2458 dwarf_output,
2459 dwarf_cleanup,
2460 NULL /* pragma list */
2461 };
2462
2463 static const struct dfmt elf64_df_stabs = {
2464 "ELF64 (x86-64) stabs (older)",
2465 "stabs",
2466 null_debug_init,
2467 stabs_linenum,
2468 null_debug_deflabel,
2469 NULL, /* .debug_smacros */
2470 NULL, /* .debug_include */
2471 NULL, /* .debug_mmacros */
2472 null_debug_directive,
2473 debug_typevalue,
2474 stabs_output,
2475 stabs_cleanup,
2476 NULL /* pragma list */
2477 };
2478
2479 static const struct dfmt * const elf64_debugs_arr[3] =
2480 { &elf64_df_dwarf, &elf64_df_stabs, NULL };
2481
2482 const struct ofmt of_elf64 = {
2483 "ELF64 (x86-64) (Linux, most Unix variants)",
2484 "elf64",
2485 ".o",
2486 0,
2487 64,
2488 elf64_debugs_arr,
2489 &elf64_df_dwarf,
2490 elf_stdmac,
2491 elf64_init,
2492 null_reset,
2493 elf64_out,
2494 elf_deflabel,
2495 elf_section_names,
2496 NULL,
2497 elf_sectalign,
2498 null_segbase,
2499 elf_directive,
2500 elf_cleanup,
2501 elf_pragma_list,
2502 };
2503
2504 static const struct dfmt elfx32_df_dwarf = {
2505 "ELFx32 (x86-64) dwarf (newer)",
2506 "dwarf",
2507 dwarfx32_init,
2508 dwarf_linenum,
2509 null_debug_deflabel,
2510 NULL, /* .debug_smacros */
2511 NULL, /* .debug_include */
2512 NULL, /* .debug_mmacros */
2513 null_debug_directive,
2514 debug_typevalue,
2515 dwarf_output,
2516 dwarf_cleanup,
2517 NULL /* pragma list */
2518 };
2519
2520 static const struct dfmt elfx32_df_stabs = {
2521 "ELFx32 (x86-64) stabs (older)",
2522 "stabs",
2523 null_debug_init,
2524 stabs_linenum,
2525 null_debug_deflabel,
2526 NULL, /* .debug_smacros */
2527 NULL, /* .debug_include */
2528 NULL, /* .debug_mmacros */
2529 null_debug_directive,
2530 debug_typevalue,
2531 stabs_output,
2532 stabs_cleanup,
2533 elf_pragma_list,
2534 };
2535
2536 static const struct dfmt * const elfx32_debugs_arr[3] =
2537 { &elfx32_df_dwarf, &elfx32_df_stabs, NULL };
2538
2539 const struct ofmt of_elfx32 = {
2540 "ELFx32 (ELF32 for x86-64) (Linux)",
2541 "elfx32",
2542 ".o",
2543 0,
2544 64,
2545 elfx32_debugs_arr,
2546 &elfx32_df_dwarf,
2547 elf_stdmac,
2548 elfx32_init,
2549 null_reset,
2550 elfx32_out,
2551 elf_deflabel,
2552 elf_section_names,
2553 NULL,
2554 elf_sectalign,
2555 null_segbase,
2556 elf_directive,
2557 elf_cleanup,
2558 NULL /* pragma list */
2559 };
2560
2561 static bool is_elf64(void)
2562 {
2563 return ofmt == &of_elf64;
2564 }
2565
2566 static bool is_elf32(void)
2567 {
2568 return ofmt == &of_elf32;
2569 }
2570
2571 static bool is_elfx32(void)
2572 {
2573 return ofmt == &of_elfx32;
2574 }
2575
2576 static bool dfmt_is_stabs(void)
2577 {
2578 return dfmt == &elf32_df_stabs ||
2579 dfmt == &elfx32_df_stabs ||
2580 dfmt == &elf64_df_stabs;
2581 }
2582
2583 static bool dfmt_is_dwarf(void)
2584 {
2585 return dfmt == &elf32_df_dwarf ||
2586 dfmt == &elfx32_df_dwarf ||
2587 dfmt == &elf64_df_dwarf;
2588 }
2589
2590 /* common debugging routines */
2591 static void debug_typevalue(int32_t type)
2592 {
2593 int32_t stype, ssize;
2594 switch (TYM_TYPE(type)) {
2595 case TY_LABEL:
2596 ssize = 0;
2597 stype = STT_NOTYPE;
2598 break;
2599 case TY_BYTE:
2600 ssize = 1;
2601 stype = STT_OBJECT;
2602 break;
2603 case TY_WORD:
2604 ssize = 2;
2605 stype = STT_OBJECT;
2606 break;
2607 case TY_DWORD:
2608 ssize = 4;
2609 stype = STT_OBJECT;
2610 break;
2611 case TY_FLOAT:
2612 ssize = 4;
2613 stype = STT_OBJECT;
2614 break;
2615 case TY_QWORD:
2616 ssize = 8;
2617 stype = STT_OBJECT;
2618 break;
2619 case TY_TBYTE:
2620 ssize = 10;
2621 stype = STT_OBJECT;
2622 break;
2623 case TY_OWORD:
2624 ssize = 16;
2625 stype = STT_OBJECT;
2626 break;
2627 case TY_YWORD:
2628 ssize = 32;
2629 stype = STT_OBJECT;
2630 break;
2631 case TY_ZWORD:
2632 ssize = 64;
2633 stype = STT_OBJECT;
2634 break;
2635 case TY_COMMON:
2636 ssize = 0;
2637 stype = STT_COMMON;
2638 break;
2639 case TY_SEG:
2640 ssize = 0;
2641 stype = STT_SECTION;
2642 break;
2643 case TY_EXTERN:
2644 ssize = 0;
2645 stype = STT_NOTYPE;
2646 break;
2647 case TY_EQU:
2648 ssize = 0;
2649 stype = STT_NOTYPE;
2650 break;
2651 default:
2652 ssize = 0;
2653 stype = STT_NOTYPE;
2654 break;
2655 }
2656 /* Set type and size info on most recently seen symbol if we haven't set it already.
2657 But avoid setting size info on object (data) symbols in absolute sections (which
2658 is primarily structs); some environments get confused with non-zero-extent absolute
2659 object symbols and end up showing them in backtraces for NULL fn pointer calls. */
2660 if (stype == STT_OBJECT && lastsym && !lastsym->type && lastsym->section != XSHN_ABS) {
2661 lastsym->size = ssize;
2662 lastsym->type = stype;
2663 }
2664 }
2665
2666 /* stabs debugging routines */
2667
2668 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t segto)
2669 {
2670 (void)segto;
2671 if (!stabs_filename) {
2672 stabs_filename = nasm_malloc(strlen(filename) + 1);
2673 strcpy(stabs_filename, filename);
2674 } else {
2675 if (strcmp(stabs_filename, filename)) {
2676 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2677 in fact, this leak comes in quite handy to maintain a list of files
2678 encountered so far in the symbol lines... */
2679
2680 /* why not nasm_free(stabs_filename); we're done with the old one */
2681
2682 stabs_filename = nasm_malloc(strlen(filename) + 1);
2683 strcpy(stabs_filename, filename);
2684 }
2685 }
2686 debug_immcall = 1;
2687 currentline = linenumber;
2688 }
2689
2690 static void stabs_output(int type, void *param)
2691 {
2692 struct symlininfo *s;
2693 struct linelist *el;
2694 if (type == TY_DEBUGSYMLIN) {
2695 if (debug_immcall) {
2696 s = (struct symlininfo *)param;
2697 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2698 return; /* line info is only collected for executable sections */
2699 numlinestabs++;
2700 el = nasm_malloc(sizeof(struct linelist));
2701 el->info.offset = s->offset;
2702 el->info.section = s->section;
2703 el->info.name = s->name;
2704 el->line = currentline;
2705 el->filename = stabs_filename;
2706 el->next = 0;
2707 if (stabslines) {
2708 stabslines->last->next = el;
2709 stabslines->last = el;
2710 } else {
2711 stabslines = el;
2712 stabslines->last = el;
2713 }
2714 }
2715 }
2716 debug_immcall = 0;
2717 }
2718
2719 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2720
2721 static void stabs_generate(void)
2722 {
2723 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
2724 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
2725 char **allfiles;
2726 int *fileidx;
2727
2728 struct linelist *ptr;
2729
2730 ptr = stabslines;
2731
2732 allfiles = nasm_zalloc(numlinestabs * sizeof(char *));
2733 numfiles = 0;
2734 while (ptr) {
2735 if (numfiles == 0) {
2736 allfiles[0] = ptr->filename;
2737 numfiles++;
2738 } else {
2739 for (i = 0; i < numfiles; i++) {
2740 if (!strcmp(allfiles[i], ptr->filename))
2741 break;
2742 }
2743 if (i >= numfiles) {
2744 allfiles[i] = ptr->filename;
2745 numfiles++;
2746 }
2747 }
2748 ptr = ptr->next;
2749 }
2750 strsize = 1;
2751 fileidx = nasm_malloc(numfiles * sizeof(int));
2752 for (i = 0; i < numfiles; i++) {
2753 fileidx[i] = strsize;
2754 strsize += strlen(allfiles[i]) + 1;
2755 }
2756 currfile = mainfileindex = 0;
2757 for (i = 0; i < numfiles; i++) {
2758 if (!strcmp(allfiles[i], elf_module)) {
2759 currfile = mainfileindex = i;
2760 break;
2761 }
2762 }
2763
2764 /*
2765 * worst case size of the stab buffer would be:
2766 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2767 * plus one "ending" entry
2768 */
2769 sbuf = nasm_malloc((numlinestabs * 2 + 4) *
2770 sizeof(struct stabentry));
2771 ssbuf = nasm_malloc(strsize);
2772 rbuf = nasm_malloc(numlinestabs * (is_elf64() ? 16 : 8) * (2 + 3));
2773 rptr = rbuf;
2774
2775 for (i = 0; i < numfiles; i++)
2776 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
2777 ssbuf[0] = 0;
2778
2779 stabstrlen = strsize; /* set global variable for length of stab strings */
2780
2781 sptr = sbuf;
2782 ptr = stabslines;
2783 numstabs = 0;
2784
2785 if (ptr) {
2786 /*
2787 * this is the first stab, its strx points to the filename of the
2788 * source-file, the n_desc field should be set to the number
2789 * of remaining stabs
2790 */
2791 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
2792
2793 /* this is the stab for the main source file */
2794 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
2795
2796 /* relocation table entry */
2797
2798 /*
2799 * Since the symbol table has two entries before
2800 * the section symbols, the index in the info.section
2801 * member must be adjusted by adding 2
2802 */
2803
2804 if (is_elf32()) {
2805 WRITELONG(rptr, (sptr - sbuf) - 4);
2806 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2807 } else if (is_elfx32()) {
2808 WRITELONG(rptr, (sptr - sbuf) - 4);
2809 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2810 WRITELONG(rptr, 0);
2811 } else {
2812 nasm_assert(is_elf64());
2813 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2814 WRITELONG(rptr, R_X86_64_32);
2815 WRITELONG(rptr, ptr->info.section + 2);
2816 WRITEDLONG(rptr, 0);
2817 }
2818 numstabs++;
2819 }
2820
2821 if (is_elf32()) {
2822 while (ptr) {
2823 if (strcmp(allfiles[currfile], ptr->filename)) {
2824 /* oops file has changed... */
2825 for (i = 0; i < numfiles; i++)
2826 if (!strcmp(allfiles[i], ptr->filename))
2827 break;
2828 currfile = i;
2829 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2830 ptr->info.offset);
2831 numstabs++;
2832
2833 /* relocation table entry */
2834 WRITELONG(rptr, (sptr - sbuf) - 4);
2835 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2836 }
2837
2838 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2839 numstabs++;
2840
2841 /* relocation table entry */
2842 WRITELONG(rptr, (sptr - sbuf) - 4);
2843 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2844
2845 ptr = ptr->next;
2846 }
2847 } else if (is_elfx32()) {
2848 while (ptr) {
2849 if (strcmp(allfiles[currfile], ptr->filename)) {
2850 /* oops file has changed... */
2851 for (i = 0; i < numfiles; i++)
2852 if (!strcmp(allfiles[i], ptr->filename))
2853 break;
2854 currfile = i;
2855 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2856 ptr->info.offset);
2857 numstabs++;
2858
2859 /* relocation table entry */
2860 WRITELONG(rptr, (sptr - sbuf) - 4);
2861 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2862 WRITELONG(rptr, ptr->info.offset);
2863 }
2864
2865 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2866 numstabs++;
2867
2868 /* relocation table entry */
2869 WRITELONG(rptr, (sptr - sbuf) - 4);
2870 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2871 WRITELONG(rptr, ptr->info.offset);
2872
2873 ptr = ptr->next;
2874 }
2875 } else {
2876 nasm_assert(is_elf64());
2877 while (ptr) {
2878 if (strcmp(allfiles[currfile], ptr->filename)) {
2879 /* oops file has changed... */
2880 for (i = 0; i < numfiles; i++)
2881 if (!strcmp(allfiles[i], ptr->filename))
2882 break;
2883 currfile = i;
2884 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2885 ptr->info.offset);
2886 numstabs++;
2887
2888 /* relocation table entry */
2889 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2890 WRITELONG(rptr, R_X86_64_32);
2891 WRITELONG(rptr, ptr->info.section + 2);
2892 WRITEDLONG(rptr, ptr->info.offset);
2893 }
2894
2895 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2896 numstabs++;
2897
2898 /* relocation table entry */
2899 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2900 WRITELONG(rptr, R_X86_64_32);
2901 WRITELONG(rptr, ptr->info.section + 2);
2902 WRITEDLONG(rptr, ptr->info.offset);
2903
2904 ptr = ptr->next;
2905 }
2906 }
2907
2908 /* this is an "ending" token */
2909 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
2910 numstabs++;
2911
2912 ((struct stabentry *)sbuf)->n_desc = numstabs;
2913
2914 nasm_free(allfiles);
2915 nasm_free(fileidx);
2916
2917 stablen = (sptr - sbuf);
2918 stabrellen = (rptr - rbuf);
2919 stabrelbuf = rbuf;
2920 stabbuf = sbuf;
2921 stabstrbuf = ssbuf;
2922 }
2923
2924 static void stabs_cleanup(void)
2925 {
2926 struct linelist *ptr, *del;
2927 if (!stabslines)
2928 return;
2929
2930 ptr = stabslines;
2931 while (ptr) {
2932 del = ptr;
2933 ptr = ptr->next;
2934 nasm_free(del);
2935 }
2936
2937 nasm_free(stabbuf);
2938 nasm_free(stabrelbuf);
2939 nasm_free(stabstrbuf);
2940 }
2941
2942 /* dwarf routines */
2943
2944 static void dwarf_init_common(const struct dwarf_format *fmt)
2945 {
2946 dwfmt = fmt;
2947 ndebugs = 3; /* 3 debug symbols */
2948 }
2949
2950 static void dwarf32_init(void)
2951 {
2952 static const struct dwarf_format dwfmt32 = {
2953 2, /* DWARF 2 */
2954 /* section version numbers: */
2955 { 2, /* .debug_aranges */
2956 0, /* .rela.debug_aranges */
2957 2, /* .debug_pubnames */
2958 2, /* .debug_info */
2959 0, /* .rela.debug_info */
2960 0, /* .debug_abbrev */
2961 2, /* .debug_line */
2962 0, /* .rela.debug_line */
2963 1, /* .debug_frame */
2964 0 } /* .debug_loc */
2965 };
2966 dwarf_init_common(&dwfmt32);
2967 }
2968
2969 static void dwarfx32_init(void)
2970 {
2971 static const struct dwarf_format dwfmtx32 = {
2972 3, /* DWARF 3 */
2973 /* section version numbers: */
2974 { 2, /* .debug_aranges */
2975 0, /* .rela.debug_aranges */
2976 2, /* .debug_pubnames */
2977 3, /* .debug_info */
2978 0, /* .rela.debug_info */
2979 0, /* .debug_abbrev */
2980 3, /* .debug_line */
2981 0, /* .rela.debug_line */
2982 3, /* .debug_frame */
2983 0 } /* .debug_loc */
2984 };
2985 dwarf_init_common(&dwfmtx32);
2986 }
2987
2988 static void dwarf64_init(void)
2989 {
2990 static const struct dwarf_format dwfmt64 = {
2991 3, /* DWARF 3 */
2992 /* section version numbers: */
2993 { 2, /* .debug_aranges */
2994 0, /* .rela.debug_aranges */
2995 2, /* .debug_pubnames */
2996 3, /* .debug_info */
2997 0, /* .rela.debug_info */
2998 0, /* .debug_abbrev */
2999 3, /* .debug_line */
3000 0, /* .rela.debug_line */
3001 3, /* .debug_frame */
3002 0 } /* .debug_loc */
3003 };
3004 dwarf_init_common(&dwfmt64);
3005 }
3006
3007 static void dwarf_linenum(const char *filename, int32_t linenumber,
3008 int32_t segto)
3009 {
3010 (void)segto;
3011 dwarf_findfile(filename);
3012 debug_immcall = 1;
3013 currentline = linenumber;
3014 }
3015
3016 /* called from elf_out with type == TY_DEBUGSYMLIN */
3017 static void dwarf_output(int type, void *param)
3018 {
3019 int ln, aa, inx, maxln, soc;
3020 struct symlininfo *s;
3021 struct SAA *plinep;
3022
3023 (void)type;
3024
3025 s = (struct symlininfo *)param;
3026
3027 /* line number info is only gathered for executable sections */
3028 if (!(sects[s->section]->flags & SHF_EXECINSTR))
3029 return;
3030
3031 /* Check if section index has changed */
3032 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
3033 dwarf_findsect(s->section);
3034
3035 /* do nothing unless line or file has changed */
3036 if (!debug_immcall)
3037 return;
3038
3039 ln = currentline - dwarf_csect->line;
3040 aa = s->offset - dwarf_csect->offset;
3041 inx = dwarf_clist->line;
3042 plinep = dwarf_csect->psaa;
3043 /* check for file change */
3044 if (!(inx == dwarf_csect->file)) {
3045 saa_write8(plinep,DW_LNS_set_file);
3046 saa_write8(plinep,inx);
3047 dwarf_csect->file = inx;
3048 }
3049 /* check for line change */
3050 if (ln) {
3051 /* test if in range of special op code */
3052 maxln = line_base + line_range;
3053 soc = (ln - line_base) + (line_range * aa) + opcode_base;
3054 if (ln >= line_base && ln < maxln && soc < 256) {
3055 saa_write8(plinep,soc);
3056 } else {
3057 saa_write8(plinep,DW_LNS_advance_line);
3058 saa_wleb128s(plinep,ln);
3059 if (aa) {
3060 saa_write8(plinep,DW_LNS_advance_pc);
3061 saa_wleb128u(plinep,aa);
3062 }
3063 saa_write8(plinep,DW_LNS_copy);
3064 }
3065 dwarf_csect->line = currentline;
3066 dwarf_csect->offset = s->offset;
3067 }
3068
3069 /* show change handled */
3070 debug_immcall = 0;
3071 }
3072
3073
3074 static void dwarf_generate(void)
3075 {
3076 uint8_t *pbuf;
3077 int indx;
3078 struct linelist *ftentry;
3079 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
3080 struct SAA *parangesrel, *plinesrel, *pinforel;
3081 struct sectlist *psect;
3082 size_t saalen, linepoff, totlen, highaddr;
3083
3084 if (is_elf32()) {
3085 /* write epilogues for each line program range */
3086 /* and build aranges section */
3087 paranges = saa_init(1L);
3088 parangesrel = saa_init(1L);
3089 saa_write16(paranges, dwfmt->sect_version[DWARF_ARANGES]);
3090 saa_write32(parangesrel, paranges->datalen+4);
3091 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
3092 saa_write32(paranges,0); /* offset into info */
3093 saa_write8(paranges,4); /* pointer size */
3094 saa_write8(paranges,0); /* not segmented */
3095 saa_write32(paranges,0); /* padding */
3096 /* iterate though sectlist entries */
3097 psect = dwarf_fsect;
3098 totlen = 0;
3099 highaddr = 0;
3100 for (indx = 0; indx < dwarf_nsections; indx++) {
3101 plinep = psect->psaa;
3102 /* Line Number Program Epilogue */
3103 saa_write8(plinep,2); /* std op 2 */
3104 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
3105 saa_write8(plinep,DW_LNS_extended_op);
3106 saa_write8(plinep,1); /* operand length */
3107 saa_write8(plinep,DW_LNE_end_sequence);
3108 totlen += plinep->datalen;
3109 /* range table relocation entry */
3110 saa_write32(parangesrel, paranges->datalen + 4);
3111 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
3112 /* range table entry */
3113 saa_write32(paranges,0x0000); /* range start */
3114 saa_write32(paranges,sects[psect->section]->len); /* range length */
3115 highaddr += sects[psect->section]->len;
3116 /* done with this entry */
3117 psect = psect->next;
3118 }
3119 saa_write32(paranges,0); /* null address */
3120 saa_write32(paranges,0); /* null length */
3121 saalen = paranges->datalen;
3122 arangeslen = saalen + 4;
3123 arangesbuf = pbuf = nasm_malloc(arangeslen);
3124 WRITELONG(pbuf,saalen); /* initial length */
3125 saa_rnbytes(paranges, pbuf, saalen);
3126 saa_free(paranges);
3127 } else if (is_elfx32()) {
3128 /* write epilogues for each line program range */
3129 /* and build aranges section */
3130 paranges = saa_init(1L);
3131 parangesrel = saa_init(1L);
3132 saa_write16(paranges, dwfmt->sect_version[DWARF_ARANGES]);
3133 saa_write32(parangesrel, paranges->datalen+4);
3134 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
3135 saa_write32(parangesrel, 0);
3136 saa_write32(paranges,0); /* offset into info */
3137 saa_write8(paranges,4); /* pointer size */
3138 saa_write8(paranges,0); /* not segmented */
3139 saa_write32(paranges,0); /* padding */
3140 /* iterate though sectlist entries */
3141 psect = dwarf_fsect;
3142 totlen = 0;
3143 highaddr = 0;
3144 for (indx = 0; indx < dwarf_nsections; indx++) {
3145 plinep = psect->psaa;
3146 /* Line Number Program Epilogue */
3147 saa_write8(plinep,2); /* std op 2 */
3148 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
3149 saa_write8(plinep,DW_LNS_extended_op);
3150 saa_write8(plinep,1); /* operand length */
3151 saa_write8(plinep,DW_LNE_end_sequence);
3152 totlen += plinep->datalen;
3153 /* range table relocation entry */
3154 saa_write32(parangesrel, paranges->datalen + 4);
3155 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
3156 saa_write32(parangesrel, (uint32_t) 0);
3157 /* range table entry */
3158 saa_write32(paranges,0x0000); /* range start */
3159 saa_write32(paranges,sects[psect->section]->len); /* range length */
3160 highaddr += sects[psect->section]->len;
3161 /* done with this entry */
3162 psect = psect->next;
3163 }
3164 saa_write32(paranges,0); /* null address */
3165 saa_write32(paranges,0); /* null length */
3166 saalen = paranges->datalen;
3167 arangeslen = saalen + 4;
3168 arangesbuf = pbuf = nasm_malloc(arangeslen);
3169 WRITELONG(pbuf,saalen); /* initial length */
3170 saa_rnbytes(paranges, pbuf, saalen);
3171 saa_free(paranges);
3172 } else {
3173 nasm_assert(is_elf64());
3174 /* write epilogues for each line program range */
3175 /* and build aranges section */
3176 paranges = saa_init(1L);
3177 parangesrel = saa_init(1L);
3178 saa_write16(paranges, dwfmt->sect_version[DWARF_ARANGES]);
3179 saa_write64(parangesrel, paranges->datalen+4);
3180 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
3181 saa_write64(parangesrel, 0);
3182 saa_write32(paranges,0); /* offset into info */
3183 saa_write8(paranges,8); /* pointer size */
3184 saa_write8(paranges,0); /* not segmented */
3185 saa_write32(paranges,0); /* padding */
3186 /* iterate though sectlist entries */
3187 psect = dwarf_fsect;
3188 totlen = 0;
3189 highaddr = 0;
3190 for (indx = 0; indx < dwarf_nsections; indx++) {
3191 plinep = psect->psaa;
3192 /* Line Number Program Epilogue */
3193 saa_write8(plinep,2); /* std op 2 */
3194 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
3195 saa_write8(plinep,DW_LNS_extended_op);
3196 saa_write8(plinep,1); /* operand length */
3197 saa_write8(plinep,DW_LNE_end_sequence);
3198 totlen += plinep->datalen;
3199 /* range table relocation entry */
3200 saa_write64(parangesrel, paranges->datalen + 4);
3201 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
3202 saa_write64(parangesrel, (uint64_t) 0);
3203 /* range table entry */
3204 saa_write64(paranges,0x0000); /* range start */
3205 saa_write64(paranges,sects[psect->section]->len); /* range length */
3206 highaddr += sects[psect->section]->len;
3207 /* done with this entry */
3208 psect = psect->next;
3209 }
3210 saa_write64(paranges,0); /* null address */
3211 saa_write64(paranges,0); /* null length */
3212 saalen = paranges->datalen;
3213 arangeslen = saalen + 4;
3214 arangesbuf = pbuf = nasm_malloc(arangeslen);
3215 WRITELONG(pbuf,saalen); /* initial length */
3216 saa_rnbytes(paranges, pbuf, saalen);
3217 saa_free(paranges);
3218 }
3219
3220 /* build rela.aranges section */
3221 arangesrellen = saalen = parangesrel->datalen;
3222 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
3223 saa_rnbytes(parangesrel, pbuf, saalen);
3224 saa_free(parangesrel);
3225
3226 /* build pubnames section */
3227 if (0) {
3228 ppubnames = saa_init(1L);
3229 saa_write16(ppubnames,dwfmt->sect_version[DWARF_PUBNAMES]);
3230 saa_write32(ppubnames,0); /* offset into info */
3231 saa_write32(ppubnames,0); /* space used in info */
3232 saa_write32(ppubnames,0); /* end of list */
3233 saalen = ppubnames->datalen;
3234 pubnameslen = saalen + 4;
3235 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
3236 WRITELONG(pbuf,saalen); /* initial length */
3237 saa_rnbytes(ppubnames, pbuf, saalen);
3238 saa_free(ppubnames);
3239 } else {
3240 /* Don't write a section without actual information */
3241 pubnameslen = 0;
3242 }
3243
3244 if (is_elf32()) {
3245 /* build info section */
3246 pinfo = saa_init(1L);
3247 pinforel = saa_init(1L);
3248 saa_write16(pinfo, dwfmt->sect_version[DWARF_INFO]);
3249 saa_write32(pinforel, pinfo->datalen + 4);
3250 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
3251 saa_write32(pinfo,0); /* offset into abbrev */
3252 saa_write8(pinfo,4); /* pointer size */
3253 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3254 saa_write32(pinforel, pinfo->datalen + 4);
3255 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3256 saa_write32(pinfo,0); /* DW_AT_low_pc */
3257 saa_write32(pinforel, pinfo->datalen + 4);
3258 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3259 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
3260 saa_write32(pinforel, pinfo->datalen + 4);
3261 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
3262 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3263 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1); /* DW_AT_name */
3264 saa_wbytes(pinfo, elf_dir, strlen(elf_dir)+1); /* DW_AT_comp_dir */
3265 saa_wbytes(pinfo, nasm_signature(), nasm_signature_len()+1);
3266 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3267 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3268 saa_write32(pinforel, pinfo->datalen + 4);
3269 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3270 saa_write32(pinfo,0); /* DW_AT_low_pc */
3271 saa_write8(pinfo,0); /* DW_AT_frame_base */
3272 saa_write8(pinfo,0); /* end of entries */
3273 saalen = pinfo->datalen;
3274 infolen = saalen + 4;
3275 infobuf = pbuf = nasm_malloc(infolen);
3276 WRITELONG(pbuf,saalen); /* initial length */
3277 saa_rnbytes(pinfo, pbuf, saalen);
3278 saa_free(pinfo);
3279 } else if (is_elfx32()) {
3280 /* build info section */
3281 pinfo = saa_init(1L);
3282 pinforel = saa_init(1L);
3283 saa_write16(pinfo, dwfmt->sect_version[DWARF_INFO]);
3284 saa_write32(pinforel, pinfo->datalen + 4);
3285 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
3286 saa_write32(pinforel, 0);
3287 saa_write32(pinfo,0); /* offset into abbrev */
3288 saa_write8(pinfo,4); /* pointer size */
3289 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3290 saa_write32(pinforel, pinfo->datalen + 4);
3291 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3292 saa_write32(pinforel, 0);
3293 saa_write32(pinfo,0); /* DW_AT_low_pc */
3294 saa_write32(pinforel, pinfo->datalen + 4);
3295 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3296 saa_write32(pinforel, highaddr);
3297 saa_write32(pinfo,0); /* DW_AT_high_pc */
3298 saa_write32(pinforel, pinfo->datalen + 4);
3299 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
3300 saa_write32(pinforel, 0);
3301 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3302 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1); /* DW_AT_name */
3303 saa_wbytes(pinfo, elf_dir, strlen(elf_dir)+1); /* DW_AT_comp_dir */
3304 saa_wbytes(pinfo, nasm_signature(), nasm_signature_len()+1);
3305 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3306 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3307 saa_write32(pinforel, pinfo->datalen + 4);
3308 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3309 saa_write32(pinforel, 0);
3310 saa_write32(pinfo,0); /* DW_AT_low_pc */
3311 saa_write8(pinfo,0); /* DW_AT_frame_base */
3312 saa_write8(pinfo,0); /* end of entries */
3313 saalen = pinfo->datalen;
3314 infolen = saalen + 4;
3315 infobuf = pbuf = nasm_malloc(infolen);
3316 WRITELONG(pbuf,saalen); /* initial length */
3317 saa_rnbytes(pinfo, pbuf, saalen);
3318 saa_free(pinfo);
3319 } else {
3320 nasm_assert(is_elf64());
3321 /* build info section */
3322 pinfo = saa_init(1L);
3323 pinforel = saa_init(1L);
3324 saa_write16(pinfo, dwfmt->sect_version[DWARF_INFO]);
3325 saa_write64(pinforel, pinfo->datalen + 4);
3326 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
3327 saa_write64(pinforel, 0);
3328 saa_write32(pinfo,0); /* offset into abbrev */
3329 saa_write8(pinfo,8); /* pointer size */
3330 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3331 saa_write64(pinforel, pinfo->datalen + 4);
3332 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3333 saa_write64(pinforel, 0);
3334 saa_write64(pinfo,0); /* DW_AT_low_pc */
3335 saa_write64(pinforel, pinfo->datalen + 4);
3336 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3337 saa_write64(pinforel, highaddr);
3338 saa_write64(pinfo,0); /* DW_AT_high_pc */
3339 saa_write64(pinforel, pinfo->datalen + 4);
3340 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
3341 saa_write64(pinforel, 0);
3342 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3343 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1); /* DW_AT_name */
3344 saa_wbytes(pinfo, elf_dir, strlen(elf_dir)+1); /* DW_AT_comp_dir */
3345 saa_wbytes(pinfo, nasm_signature(), nasm_signature_len()+1);
3346 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3347 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3348 saa_write64(pinforel, pinfo->datalen + 4);
3349 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3350 saa_write64(pinforel, 0);
3351 saa_write64(pinfo,0); /* DW_AT_low_pc */
3352 saa_write8(pinfo,0); /* DW_AT_frame_base */
3353 saa_write8(pinfo,0); /* end of entries */
3354 saalen = pinfo->datalen;
3355 infolen = saalen + 4;
3356 infobuf = pbuf = nasm_malloc(infolen);
3357 WRITELONG(pbuf,saalen); /* initial length */
3358 saa_rnbytes(pinfo, pbuf, saalen);
3359 saa_free(pinfo);
3360 }
3361
3362 /* build rela.info section */
3363 inforellen = saalen = pinforel->datalen;
3364 inforelbuf = pbuf = nasm_malloc(inforellen);
3365 saa_rnbytes(pinforel, pbuf, saalen);
3366 saa_free(pinforel);
3367
3368 /* build abbrev section */
3369 pabbrev = saa_init(1L);
3370 saa_write8(pabbrev,1); /* entry number LEB128u */
3371 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
3372 saa_write8(pabbrev,1); /* has children */
3373 /* the following attributes and forms are all LEB128u values */
3374 saa_write8(pabbrev,DW_AT_low_pc);
3375 saa_write8(pabbrev,DW_FORM_addr);
3376 saa_write8(pabbrev,DW_AT_high_pc);
3377 saa_write8(pabbrev,DW_FORM_addr);
3378 saa_write8(pabbrev,DW_AT_stmt_list);
3379 saa_write8(pabbrev,DW_FORM_data4);
3380 saa_write8(pabbrev,DW_AT_name);
3381 saa_write8(pabbrev,DW_FORM_string);
3382 saa_write8(pabbrev,DW_AT_comp_dir);
3383 saa_write8(pabbrev,DW_FORM_string);
3384 saa_write8(pabbrev,DW_AT_producer);
3385 saa_write8(pabbrev,DW_FORM_string);
3386 saa_write8(pabbrev,DW_AT_language);
3387 saa_write8(pabbrev,DW_FORM_data2);
3388 saa_write16(pabbrev,0); /* end of entry */
3389 /* LEB128u usage same as above */
3390 saa_write8(pabbrev,2); /* entry number */
3391 saa_write8(pabbrev,DW_TAG_subprogram);
3392 saa_write8(pabbrev,0); /* no children */
3393 saa_write8(pabbrev,DW_AT_low_pc);
3394 saa_write8(pabbrev,DW_FORM_addr);
3395 saa_write8(pabbrev,DW_AT_frame_base);
3396 saa_write8(pabbrev,DW_FORM_exprloc);
3397 saa_write16(pabbrev,0); /* end of entry */
3398 /* Terminal zero entry */
3399 saa_write8(pabbrev,0);
3400 abbrevlen = saalen = pabbrev->datalen;
3401 abbrevbuf = pbuf = nasm_malloc(saalen);
3402 saa_rnbytes(pabbrev, pbuf, saalen);
3403 saa_free(pabbrev);
3404
3405 /* build line section */
3406 /* prolog */
3407 plines = saa_init(1L);
3408 saa_write8(plines,1); /* Minimum Instruction Length */
3409 saa_write8(plines,1); /* Initial value of 'is_stmt' */
3410 saa_write8(plines,line_base); /* Line Base */
3411 saa_write8(plines,line_range); /* Line Range */
3412 saa_write8(plines,opcode_base); /* Opcode Base */
3413 /* standard opcode lengths (# of LEB128u operands) */
3414 saa_write8(plines,0); /* Std opcode 1 length */
3415 saa_write8(plines,1); /* Std opcode 2 length */
3416 saa_write8(plines,1); /* Std opcode 3 length */
3417 saa_write8(plines,1); /* Std opcode 4 length */
3418 saa_write8(plines,1); /* Std opcode 5 length */
3419 saa_write8(plines,0); /* Std opcode 6 length */
3420 saa_write8(plines,0); /* Std opcode 7 length */
3421 saa_write8(plines,0); /* Std opcode 8 length */
3422 saa_write8(plines,1); /* Std opcode 9 length */
3423 saa_write8(plines,0); /* Std opcode 10 length */
3424 saa_write8(plines,0); /* Std opcode 11 length */
3425 saa_write8(plines,1); /* Std opcode 12 length */
3426 /* Directory Table */
3427 saa_write8(plines,0); /* End of table */
3428 /* File Name Table */
3429 ftentry = dwarf_flist;
3430 for (indx = 0; indx < dwarf_numfiles; indx++) {
3431 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
3432 saa_write8(plines,0); /* directory LEB128u */
3433 saa_write8(plines,0); /* time LEB128u */
3434 saa_write8(plines,0); /* size LEB128u */
3435 ftentry = ftentry->next;
3436 }
3437 saa_write8(plines,0); /* End of table */
3438 linepoff = plines->datalen;
3439 linelen = linepoff + totlen + 10;
3440 linebuf = pbuf = nasm_malloc(linelen);
3441 WRITELONG(pbuf,linelen-4); /* initial length */
3442 WRITESHORT(pbuf,dwfmt->sect_version[DWARF_LINE]);
3443 WRITELONG(pbuf,linepoff); /* offset to line number program */
3444 /* write line header */
3445 saalen = linepoff;
3446 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
3447 pbuf += linepoff;
3448 saa_free(plines);
3449 /* concatenate line program ranges */
3450 linepoff += 13;
3451 plinesrel = saa_init(1L);
3452 psect = dwarf_fsect;
3453 if (is_elf32()) {
3454 for (indx = 0; indx < dwarf_nsections; indx++) {
3455 saa_write32(plinesrel, linepoff);
3456 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
3457 plinep = psect->psaa;
3458 saalen = plinep->datalen;
3459 saa_rnbytes(plinep, pbuf, saalen);
3460 pbuf += saalen;
3461 linepoff += saalen;
3462 saa_free(plinep);
3463 /* done with this entry */
3464 psect = psect->next;
3465 }
3466 } else if (is_elfx32()) {
3467 for (indx = 0; indx < dwarf_nsections; indx++) {
3468 saa_write32(plinesrel, linepoff);
3469 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
3470 saa_write32(plinesrel, 0);
3471 plinep = psect->psaa;
3472 saalen = plinep->datalen;
3473 saa_rnbytes(plinep, pbuf, saalen);
3474 pbuf += saalen;
3475 linepoff += saalen;
3476 saa_free(plinep);
3477 /* done with this entry */
3478 psect = psect->next;
3479 }
3480 } else {
3481 nasm_assert(is_elf64());
3482 for (indx = 0; indx < dwarf_nsections; indx++) {
3483 saa_write64(plinesrel, linepoff);
3484 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
3485 saa_write64(plinesrel, (uint64_t) 0);
3486 plinep = psect->psaa;
3487 saalen = plinep->datalen;
3488 saa_rnbytes(plinep, pbuf, saalen);
3489 pbuf += saalen;
3490 linepoff += saalen;
3491 saa_free(plinep);
3492 /* done with this entry */
3493 psect = psect->next;
3494 }
3495 }
3496
3497 /* build rela.lines section */
3498 linerellen =saalen = plinesrel->datalen;
3499 linerelbuf = pbuf = nasm_malloc(linerellen);
3500 saa_rnbytes(plinesrel, pbuf, saalen);
3501 saa_free(plinesrel);
3502
3503 /* build .debug_frame section */
3504 if (0) {
3505 framelen = 4;
3506 framebuf = pbuf = nasm_malloc(framelen);
3507 WRITELONG(pbuf,framelen-4); /* initial length */
3508 } else {
3509 /* Leave .debug_frame empty if not used! */
3510 framelen = 0;
3511 }
3512
3513 /* build .debug_loc section */
3514 if (0) {
3515 loclen = 16;
3516 locbuf = pbuf = nasm_malloc(loclen);
3517 if (is_elf32() || is_elfx32()) {
3518 WRITELONG(pbuf,0); /* null beginning offset */
3519 WRITELONG(pbuf,0); /* null ending offset */
3520 } else {
3521 nasm_assert(is_elf64());
3522 WRITEDLONG(pbuf,0); /* null beginning offset */
3523 WRITEDLONG(pbuf,0); /* null ending offset */
3524 }
3525 } else {
3526 /* Leave .debug_frame empty if not used! */
3527 loclen = 0;
3528 }
3529 }
3530
3531 static void dwarf_cleanup(void)
3532 {
3533 nasm_free(arangesbuf);
3534 nasm_free(arangesrelbuf);
3535 nasm_free(pubnamesbuf);
3536 nasm_free(infobuf);
3537 nasm_free(inforelbuf);
3538 nasm_free(abbrevbuf);
3539 nasm_free(linebuf);
3540 nasm_free(linerelbuf);
3541 nasm_free(framebuf);
3542 nasm_free(locbuf);
3543 }
3544
3545 static void dwarf_findfile(const char * fname)
3546 {
3547 int finx;
3548 struct linelist *match;
3549
3550 /* return if fname is current file name */
3551 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
3552 return;
3553
3554 /* search for match */
3555 match = 0;
3556 if (dwarf_flist) {
3557 match = dwarf_flist;
3558 for (finx = 0; finx < dwarf_numfiles; finx++) {
3559 if (!(strcmp(fname, match->filename))) {
3560 dwarf_clist = match;
3561 return;
3562 }
3563 match = match->next;
3564 }
3565 }
3566
3567 /* add file name to end of list */
3568 dwarf_clist = nasm_malloc(sizeof(struct linelist));
3569 dwarf_numfiles++;
3570 dwarf_clist->line = dwarf_numfiles;
3571 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
3572 strcpy(dwarf_clist->filename,fname);
3573 dwarf_clist->next = 0;
3574 if (!dwarf_flist) { /* if first entry */
3575 dwarf_flist = dwarf_elist = dwarf_clist;
3576 dwarf_clist->last = 0;
3577 } else { /* chain to previous entry */
3578 dwarf_elist->next = dwarf_clist;
3579 dwarf_elist = dwarf_clist;
3580 }
3581 }
3582
3583 static void dwarf_findsect(const int index)
3584 {
3585 int sinx;
3586 struct sectlist *match;
3587 struct SAA *plinep;
3588
3589 /* return if index is current section index */
3590 if (dwarf_csect && (dwarf_csect->section == index))
3591 return;
3592
3593 /* search for match */
3594 match = 0;
3595 if (dwarf_fsect) {
3596 match = dwarf_fsect;
3597 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
3598 if (match->section == index) {
3599 dwarf_csect = match;
3600 return;
3601 }
3602 match = match->next;
3603 }
3604 }
3605
3606 /* add entry to end of list */
3607 dwarf_csect = nasm_malloc(sizeof(struct sectlist));
3608 dwarf_nsections++;
3609 dwarf_csect->psaa = plinep = saa_init(1L);
3610 dwarf_csect->line = 1;
3611 dwarf_csect->offset = 0;
3612 dwarf_csect->file = 1;
3613 dwarf_csect->section = index;
3614 dwarf_csect->next = 0;
3615 /* set relocatable address at start of line program */
3616 saa_write8(plinep,DW_LNS_extended_op);
3617 saa_write8(plinep,is_elf64() ? 9 : 5); /* operand length */
3618 saa_write8(plinep,DW_LNE_set_address);
3619 if (is_elf64())
3620 saa_write64(plinep,0); /* Start Address */
3621 else
3622 saa_write32(plinep,0); /* Start Address */
3623
3624 if (!dwarf_fsect) { /* if first entry */
3625 dwarf_fsect = dwarf_esect = dwarf_csect;
3626 dwarf_csect->last = 0;
3627 } else { /* chain to previous entry */
3628 dwarf_esect->next = dwarf_csect;
3629 dwarf_esect = dwarf_csect;
3630 }
3631 }
3632
3633 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */