Merge branch 'master' of ssh://repo.or.cz/srv/git/nasm
[nasm.git] / output / outelf.c
blob7ac2d54bfdec60c993e1758e5db61546200b8026
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * Common code for outelf32 and outelf64
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
43 #include "nasm.h"
44 #include "nasmlib.h"
45 #include "error.h"
46 #include "saa.h"
47 #include "raa.h"
48 #include "stdscan.h"
49 #include "eval.h"
50 #include "outform.h"
51 #include "outlib.h"
52 #include "rbtree.h"
53 #include "ver.h"
55 #include "dwarf.h"
56 #include "stabs.h"
57 #include "outelf.h"
58 #include "elf.h"
60 #if defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32)
62 #define SECT_DELTA 32
63 static struct elf_section **sects;
64 static int nsects, sectlen;
66 #define SHSTR_DELTA 256
67 static char *shstrtab;
68 static int shstrtablen, shstrtabsize;
70 static struct SAA *syms;
71 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
73 static int32_t def_seg;
75 static struct RAA *bsym;
77 static struct SAA *strs;
78 static uint32_t strslen;
80 static struct elf_symbol *fwds;
82 static char elf_module[FILENAME_MAX];
84 extern const struct ofmt of_elf32;
85 extern const struct ofmt of_elf64;
86 extern const struct ofmt of_elfx32;
88 static struct ELF_SECTDATA {
89 void *data;
90 int64_t len;
91 bool is_saa;
92 } *elf_sects;
94 static int elf_nsect, nsections;
95 static int64_t elf_foffs;
97 static void elf_write(void);
98 static void elf_sect_write(struct elf_section *, const void *, size_t);
99 static void elf_sect_writeaddr(struct elf_section *, int64_t, size_t);
100 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
101 int, int);
102 static void elf_write_sections(void);
103 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
104 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
105 static void add_sectname(const char *, const char *);
107 struct erel {
108 int offset;
109 int info;
112 struct symlininfo {
113 int offset;
114 int section; /* index into sects[] */
115 int segto; /* internal section number */
116 char *name; /* shallow-copied pointer of section name */
119 struct linelist {
120 struct linelist *next;
121 struct linelist *last;
122 struct symlininfo info;
123 char *filename;
124 int line;
127 struct sectlist {
128 struct SAA *psaa;
129 int section;
130 int line;
131 int offset;
132 int file;
133 struct sectlist *next;
134 struct sectlist *last;
137 /* common debug variables */
138 static int currentline = 1;
139 static int debug_immcall = 0;
141 /* stabs debug variables */
142 static struct linelist *stabslines = 0;
143 static int numlinestabs = 0;
144 static char *stabs_filename = 0;
145 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
146 static int stablen, stabstrlen, stabrellen;
148 /* dwarf debug variables */
149 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
150 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
151 static int dwarf_numfiles = 0, dwarf_nsections;
152 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
153 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
154 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
155 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
156 abbrevlen, linelen, linerellen, framelen, loclen;
157 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
159 static struct elf_symbol *lastsym;
161 /* common debugging routines */
162 static void debug_typevalue(int32_t);
164 /* stabs debugging routines */
165 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t);
166 static void stabs_output(int, void *);
167 static void stabs_generate(void);
168 static void stabs_cleanup(void);
170 /* dwarf debugging routines */
171 static void dwarf_init(void);
172 static void dwarf_linenum(const char *filename, int32_t linenumber, int32_t);
173 static void dwarf_output(int, void *);
174 static void dwarf_generate(void);
175 static void dwarf_cleanup(void);
176 static void dwarf_findfile(const char *);
177 static void dwarf_findsect(const int);
179 static bool is_elf64(void);
180 static bool is_elf32(void);
181 static bool is_elfx32(void);
183 static bool dfmt_is_stabs(void);
184 static bool dfmt_is_dwarf(void);
187 * Special NASM section numbers which are used to define ELF special
188 * symbols.
190 static int32_t elf_gotpc_sect, elf_gotoff_sect;
191 static int32_t elf_got_sect, elf_plt_sect;
192 static int32_t elf_sym_sect, elf_gottpoff_sect, elf_tlsie_sect;
194 uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
195 uint8_t elf_abiver = 0; /* Current ABI version */
197 const struct elf_known_section elf_known_sections[] = {
198 { ".text", SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, 16 },
199 { ".rodata", SHT_PROGBITS, SHF_ALLOC, 4 },
200 { ".lrodata", SHT_PROGBITS, SHF_ALLOC, 4 },
201 { ".data", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4 },
202 { ".ldata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4 },
203 { ".bss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4 },
204 { ".lbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4 },
205 { ".tdata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4 },
206 { ".tbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4 },
207 { ".comment", SHT_PROGBITS, 0, 1 },
208 { NULL, SHT_PROGBITS, SHF_ALLOC, 1 } /* default */
211 /* parse section attributes */
212 static void elf_section_attrib(char *name, char *attr, int pass,
213 uint32_t *flags_and, uint32_t *flags_or,
214 uint64_t *align, int *type)
216 char *opt, *val, *next;
218 opt = nasm_skip_spaces(attr);
219 if (!opt || !*opt)
220 return;
222 while ((opt = nasm_opt_val(opt, &val, &next))) {
223 if (!nasm_stricmp(opt, "align")) {
224 if (!val) {
225 nasm_error(ERR_NONFATAL,
226 "section align without value specified");
227 } else {
228 *align = atoi(val);
229 if (*align == 0) {
230 *align = SHA_ANY;
231 } else if (!is_power2(*align)) {
232 nasm_error(ERR_NONFATAL,
233 "section alignment %"PRId64" is not a power of two",
234 *align);
235 *align = SHA_ANY;
238 } else if (!nasm_stricmp(opt, "alloc")) {
239 *flags_and |= SHF_ALLOC;
240 *flags_or |= SHF_ALLOC;
241 } else if (!nasm_stricmp(opt, "noalloc")) {
242 *flags_and |= SHF_ALLOC;
243 *flags_or &= ~SHF_ALLOC;
244 } else if (!nasm_stricmp(opt, "exec")) {
245 *flags_and |= SHF_EXECINSTR;
246 *flags_or |= SHF_EXECINSTR;
247 } else if (!nasm_stricmp(opt, "noexec")) {
248 *flags_and |= SHF_EXECINSTR;
249 *flags_or &= ~SHF_EXECINSTR;
250 } else if (!nasm_stricmp(opt, "write")) {
251 *flags_and |= SHF_WRITE;
252 *flags_or |= SHF_WRITE;
253 } else if (!nasm_stricmp(opt, "tls")) {
254 *flags_and |= SHF_TLS;
255 *flags_or |= SHF_TLS;
256 } else if (!nasm_stricmp(opt, "nowrite")) {
257 *flags_and |= SHF_WRITE;
258 *flags_or &= ~SHF_WRITE;
259 } else if (!nasm_stricmp(opt, "progbits")) {
260 *type = SHT_PROGBITS;
261 } else if (!nasm_stricmp(opt, "nobits")) {
262 *type = SHT_NOBITS;
263 } else if (pass == 1) {
264 nasm_error(ERR_WARNING,
265 "Unknown section attribute '%s' ignored on"
266 " declaration of section `%s'", opt, name);
268 opt = next;
272 static enum directive_result
273 elf_directive(enum directive directive, char *value, int pass)
275 int64_t n;
276 bool err;
277 char *p;
279 switch (directive) {
280 case D_OSABI:
281 if (pass == 2)
282 return DIRR_OK; /* ignore in pass 2 */
284 n = readnum(value, &err);
285 if (err) {
286 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
287 return DIRR_ERROR;
290 if (n < 0 || n > 255) {
291 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
292 return DIRR_ERROR;
295 elf_osabi = n;
296 elf_abiver = 0;
298 p = strchr(value,',');
299 if (!p)
300 return DIRR_OK;
302 n = readnum(p + 1, &err);
303 if (err || n < 0 || n > 255) {
304 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
305 return DIRR_ERROR;
308 elf_abiver = n;
309 return DIRR_OK;
311 default:
312 return DIRR_UNKNOWN;
316 static void elf_init(void)
318 sects = NULL;
319 nsects = sectlen = 0;
320 syms = saa_init((int32_t)sizeof(struct elf_symbol));
321 nlocals = nglobs = ndebugs = 0;
322 bsym = raa_init();
323 strs = saa_init(1L);
324 saa_wbytes(strs, "\0", 1L);
325 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
326 strslen = 2 + strlen(elf_module);
327 shstrtab = NULL;
328 shstrtablen = shstrtabsize = 0;;
329 add_sectname("", "");
331 fwds = NULL;
334 * FIXME: tlsie is Elf32 only and
335 * gottpoff is Elfx32|64 only.
338 elf_gotpc_sect = seg_alloc();
339 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
340 elf_gotoff_sect = seg_alloc();
341 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
342 elf_got_sect = seg_alloc();
343 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
344 elf_plt_sect = seg_alloc();
345 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
346 elf_sym_sect = seg_alloc();
347 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
348 elf_gottpoff_sect = seg_alloc();
349 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
350 elf_tlsie_sect = seg_alloc();
351 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
353 def_seg = seg_alloc();
356 static void elf_cleanup(void)
358 struct elf_reloc *r;
359 int i;
361 elf_write();
362 for (i = 0; i < nsects; i++) {
363 if (sects[i]->type != SHT_NOBITS)
364 saa_free(sects[i]->data);
365 if (sects[i]->head)
366 saa_free(sects[i]->rel);
367 while (sects[i]->head) {
368 r = sects[i]->head;
369 sects[i]->head = sects[i]->head->next;
370 nasm_free(r);
373 nasm_free(sects);
374 saa_free(syms);
375 raa_free(bsym);
376 saa_free(strs);
377 dfmt->cleanup();
380 /* add entry to the elf .shstrtab section */
381 static void add_sectname(const char *firsthalf, const char *secondhalf)
383 int len = strlen(firsthalf) + strlen(secondhalf);
384 while (shstrtablen + len + 1 > shstrtabsize)
385 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
386 strcpy(shstrtab + shstrtablen, firsthalf);
387 strcat(shstrtab + shstrtablen, secondhalf);
388 shstrtablen += len + 1;
391 static int elf_make_section(char *name, int type, int flags, int align)
393 struct elf_section *s;
395 s = nasm_zalloc(sizeof(*s));
397 if (type != SHT_NOBITS)
398 s->data = saa_init(1L);
399 s->tail = &s->head;
400 if (!strcmp(name, ".text"))
401 s->index = def_seg;
402 else
403 s->index = seg_alloc();
404 add_sectname("", name);
406 s->name = nasm_strdup(name);
407 s->type = type;
408 s->flags = flags;
409 s->align = align;
411 if (nsects >= sectlen)
412 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
413 sects[nsects++] = s;
415 return nsects - 1;
418 static int32_t elf_section_names(char *name, int pass, int *bits)
420 char *p;
421 uint32_t flags, flags_and, flags_or;
422 uint64_t align;
423 int type, i;
425 if (!name) {
426 *bits = ofmt->maxbits;
427 return def_seg;
430 p = nasm_skip_word(name);
431 if (*p)
432 *p++ = '\0';
433 flags_and = flags_or = type = align = 0;
435 elf_section_attrib(name, p, pass, &flags_and,
436 &flags_or, &align, &type);
438 if (!strcmp(name, ".shstrtab") ||
439 !strcmp(name, ".symtab") ||
440 !strcmp(name, ".strtab")) {
441 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
442 "name `%s'", name);
443 return NO_SEG;
446 for (i = 0; i < nsects; i++)
447 if (!strcmp(name, sects[i]->name))
448 break;
449 if (i == nsects) {
450 const struct elf_known_section *ks = elf_known_sections;
452 while (ks->name) {
453 if (!strcmp(name, ks->name))
454 break;
455 ks++;
458 type = type ? type : ks->type;
459 align = align ? align : ks->align;
460 flags = (ks->flags & ~flags_and) | flags_or;
462 i = elf_make_section(name, type, flags, align);
463 } else if (pass == 1) {
464 if ((type && sects[i]->type != type)
465 || (align && sects[i]->align != align)
466 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
467 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
468 " redeclaration of section `%s'", name);
471 return sects[i]->index;
474 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
475 int is_global, char *special)
477 int pos = strslen;
478 struct elf_symbol *sym;
479 bool special_used = false;
481 #if defined(DEBUG) && DEBUG>2
482 nasm_error(ERR_DEBUG,
483 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
484 name, segment, offset, is_global, special);
485 #endif
486 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
488 * This is a NASM special symbol. We never allow it into
489 * the ELF symbol table, even if it's a valid one. If it
490 * _isn't_ a valid one, we should barf immediately.
492 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
494 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
495 strcmp(name, "..got") && strcmp(name, "..plt") &&
496 strcmp(name, "..sym") && strcmp(name, "..gottpoff") &&
497 strcmp(name, "..tlsie"))
498 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
499 return;
502 if (is_global == 3) {
503 struct elf_symbol **s;
505 * Fix up a forward-reference symbol size from the first
506 * pass.
508 for (s = &fwds; *s; s = &(*s)->nextfwd)
509 if (!strcmp((*s)->name, name)) {
510 struct tokenval tokval;
511 expr *e;
512 char *p = nasm_skip_spaces(nasm_skip_word(special));
514 stdscan_reset();
515 stdscan_set(p);
516 tokval.t_type = TOKEN_INVALID;
517 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
518 if (e) {
519 if (!is_simple(e))
520 nasm_error(ERR_NONFATAL, "cannot use relocatable"
521 " expression as symbol size");
522 else
523 (*s)->size = reloc_value(e);
527 * Remove it from the list of unresolved sizes.
529 nasm_free((*s)->name);
530 *s = (*s)->nextfwd;
531 return;
533 return; /* it wasn't an important one */
536 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
537 strslen += 1 + strlen(name);
539 lastsym = sym = saa_wstruct(syms);
541 memset(&sym->symv, 0, sizeof(struct rbtree));
543 sym->strpos = pos;
544 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
545 sym->other = STV_DEFAULT;
546 sym->size = 0;
547 if (segment == NO_SEG)
548 sym->section = SHN_ABS;
549 else {
550 int i;
551 sym->section = SHN_UNDEF;
552 if (segment == def_seg) {
553 /* we have to be sure at least text section is there */
554 int tempint;
555 if (segment != elf_section_names(".text", 2, &tempint))
556 nasm_panic(0, "strange segment conditions in ELF driver");
558 for (i = 0; i < nsects; i++) {
559 if (segment == sects[i]->index) {
560 sym->section = i + 1;
561 break;
566 if (is_global == 2) {
567 sym->size = offset;
568 sym->symv.key = 0;
569 sym->section = SHN_COMMON;
571 * We have a common variable. Check the special text to see
572 * if it's a valid number and power of two; if so, store it
573 * as the alignment for the common variable.
575 if (special) {
576 bool err;
577 sym->symv.key = readnum(special, &err);
578 if (err)
579 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
580 " valid number", special);
581 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
582 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
583 " power of two", special);
585 special_used = true;
586 } else
587 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
589 if (sym->type == SYM_GLOBAL) {
591 * If sym->section == SHN_ABS, then the first line of the
592 * else section would cause a core dump, because its a reference
593 * beyond the end of the section array.
594 * This behaviour is exhibited by this code:
595 * GLOBAL crash_nasm
596 * crash_nasm equ 0
597 * To avoid such a crash, such requests are silently discarded.
598 * This may not be the best solution.
600 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
601 bsym = raa_write(bsym, segment, nglobs);
602 } else if (sym->section != SHN_ABS) {
604 * This is a global symbol; so we must add it to the rbtree
605 * of global symbols in its section.
607 * In addition, we check the special text for symbol
608 * type and size information.
610 sects[sym->section-1]->gsyms =
611 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
613 if (special) {
614 int n = strcspn(special, " \t");
616 if (!nasm_strnicmp(special, "function", n))
617 sym->type |= STT_FUNC;
618 else if (!nasm_strnicmp(special, "data", n) ||
619 !nasm_strnicmp(special, "object", n))
620 sym->type |= STT_OBJECT;
621 else if (!nasm_strnicmp(special, "notype", n))
622 sym->type |= STT_NOTYPE;
623 else
624 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
625 n, special);
626 special += n;
628 special = nasm_skip_spaces(special);
629 if (*special) {
630 n = strcspn(special, " \t");
631 if (!nasm_strnicmp(special, "default", n))
632 sym->other = STV_DEFAULT;
633 else if (!nasm_strnicmp(special, "internal", n))
634 sym->other = STV_INTERNAL;
635 else if (!nasm_strnicmp(special, "hidden", n))
636 sym->other = STV_HIDDEN;
637 else if (!nasm_strnicmp(special, "protected", n))
638 sym->other = STV_PROTECTED;
639 else
640 n = 0;
641 special += n;
644 if (*special) {
645 struct tokenval tokval;
646 expr *e;
647 int fwd = 0;
648 char *saveme = stdscan_get();
650 while (special[n] && nasm_isspace(special[n]))
651 n++;
653 * We have a size expression; attempt to
654 * evaluate it.
656 stdscan_reset();
657 stdscan_set(special + n);
658 tokval.t_type = TOKEN_INVALID;
659 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
660 if (fwd) {
661 sym->nextfwd = fwds;
662 fwds = sym;
663 sym->name = nasm_strdup(name);
664 } else if (e) {
665 if (!is_simple(e))
666 nasm_error(ERR_NONFATAL, "cannot use relocatable"
667 " expression as symbol size");
668 else
669 sym->size = reloc_value(e);
671 stdscan_set(saveme);
673 special_used = true;
676 * If TLS segment, mark symbol accordingly.
678 if (sects[sym->section - 1]->flags & SHF_TLS) {
679 sym->type &= 0xf0;
680 sym->type |= STT_TLS;
683 sym->globnum = nglobs;
684 nglobs++;
685 } else
686 nlocals++;
688 if (special && !special_used)
689 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
692 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
693 int64_t offset, int type)
695 struct elf_reloc *r;
697 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
698 sect->tail = &r->next;
700 r->address = sect->len;
701 r->offset = offset;
703 if (segment != NO_SEG) {
704 int i;
705 for (i = 0; i < nsects; i++)
706 if (segment == sects[i]->index)
707 r->symbol = i + 2;
708 if (!r->symbol)
709 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
711 r->type = type;
713 sect->nrelocs++;
717 * This routine deals with ..got and ..sym relocations: the more
718 * complicated kinds. In shared-library writing, some relocations
719 * with respect to global symbols must refer to the precise symbol
720 * rather than referring to an offset from the base of the section
721 * _containing_ the symbol. Such relocations call to this routine,
722 * which searches the symbol list for the symbol in question.
724 * R_386_GOT32 | R_X86_64_GOT32 references require the _exact_ symbol address to be
725 * used; R_386_32 | R_X86_64_32 references can be at an offset from the symbol.
726 * The boolean argument `exact' tells us this.
728 * Return value is the adjusted value of `addr', having become an
729 * offset from the symbol rather than the section. Should always be
730 * zero when returning from an exact call.
732 * Limitation: if you define two symbols at the same place,
733 * confusion will occur.
735 * Inefficiency: we search, currently, using a linked list which
736 * isn't even necessarily sorted.
738 static int64_t elf_add_gsym_reloc(struct elf_section *sect,
739 int32_t segment, uint64_t offset,
740 int64_t pcrel, int type, bool exact)
742 struct elf_reloc *r;
743 struct elf_section *s;
744 struct elf_symbol *sym;
745 struct rbtree *srb;
746 int i;
749 * First look up the segment/offset pair and find a global
750 * symbol corresponding to it. If it's not one of our segments,
751 * then it must be an external symbol, in which case we're fine
752 * doing a normal elf_add_reloc after first sanity-checking
753 * that the offset from the symbol is zero.
755 s = NULL;
756 for (i = 0; i < nsects; i++)
757 if (segment == sects[i]->index) {
758 s = sects[i];
759 break;
762 if (!s) {
763 if (exact && offset)
764 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
765 else
766 elf_add_reloc(sect, segment, offset - pcrel, type);
767 return 0;
770 srb = rb_search(s->gsyms, offset);
771 if (!srb || (exact && srb->key != offset)) {
772 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
773 " for this reference");
774 return 0;
776 sym = container_of(srb, struct elf_symbol, symv);
778 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
779 sect->tail = &r->next;
781 r->next = NULL;
782 r->address = sect->len;
783 r->offset = offset - pcrel - sym->symv.key;
784 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
785 r->type = type;
787 sect->nrelocs++;
788 return r->offset;
791 static void elf32_out(int32_t segto, const void *data,
792 enum out_type type, uint64_t size,
793 int32_t segment, int32_t wrt)
795 struct elf_section *s;
796 int64_t addr;
797 int reltype, bytes;
798 int i;
799 static struct symlininfo sinfo;
802 * handle absolute-assembly (structure definitions)
804 if (segto == NO_SEG) {
805 if (type != OUT_RESERVE)
806 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
807 " space");
808 return;
811 s = NULL;
812 for (i = 0; i < nsects; i++)
813 if (segto == sects[i]->index) {
814 s = sects[i];
815 break;
817 if (!s) {
818 int tempint; /* ignored */
819 if (segto != elf_section_names(".text", 2, &tempint))
820 nasm_panic(0, "strange segment conditions in ELF driver");
821 else {
822 s = sects[nsects - 1];
823 i = nsects - 1;
827 /* again some stabs debugging stuff */
828 sinfo.offset = s->len;
829 sinfo.section = i;
830 sinfo.segto = segto;
831 sinfo.name = s->name;
832 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
833 /* end of debugging stuff */
835 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
836 nasm_error(ERR_WARNING, "attempt to initialize memory in"
837 " BSS section `%s': ignored", s->name);
838 s->len += realsize(type, size);
839 return;
842 switch (type) {
843 case OUT_RESERVE:
844 if (s->type == SHT_PROGBITS) {
845 nasm_error(ERR_WARNING, "uninitialized space declared in"
846 " non-BSS section `%s': zeroing", s->name);
847 elf_sect_write(s, NULL, size);
848 } else
849 s->len += size;
850 break;
852 case OUT_RAWDATA:
853 if (segment != NO_SEG)
854 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
855 elf_sect_write(s, data, size);
856 break;
858 case OUT_ADDRESS:
860 bool gnu16 = false;
861 int asize = abs((int)size);
863 addr = *(int64_t *)data;
864 if (segment != NO_SEG) {
865 if (segment % 2) {
866 nasm_error(ERR_NONFATAL, "ELF format does not support"
867 " segment base references");
868 } else {
869 if (wrt == NO_SEG) {
871 * The if() is a hack to deal with compilers which
872 * don't handle switch() statements with 64-bit
873 * expressions.
875 switch (asize) {
876 case 1:
877 gnu16 = true;
878 elf_add_reloc(s, segment, 0, R_386_8);
879 break;
880 case 2:
881 gnu16 = true;
882 elf_add_reloc(s, segment, 0, R_386_16);
883 break;
884 case 4:
885 elf_add_reloc(s, segment, 0, R_386_32);
886 break;
887 default: /* Error issued further down */
888 break;
890 } else if (wrt == elf_gotpc_sect + 1) {
892 * The user will supply GOT relative to $$. ELF
893 * will let us have GOT relative to $. So we
894 * need to fix up the data item by $-$$.
896 addr += s->len;
897 elf_add_reloc(s, segment, 0, R_386_GOTPC);
898 } else if (wrt == elf_gotoff_sect + 1) {
899 elf_add_reloc(s, segment, 0, R_386_GOTOFF);
900 } else if (wrt == elf_tlsie_sect + 1) {
901 addr = elf_add_gsym_reloc(s, segment, addr, 0,
902 R_386_TLS_IE, true);
903 } else if (wrt == elf_got_sect + 1) {
904 addr = elf_add_gsym_reloc(s, segment, addr, 0,
905 R_386_GOT32, true);
906 } else if (wrt == elf_sym_sect + 1) {
907 switch (asize) {
908 case 1:
909 gnu16 = true;
910 addr = elf_add_gsym_reloc(s, segment, addr, 0,
911 R_386_8, false);
912 break;
913 case 2:
914 gnu16 = true;
915 addr = elf_add_gsym_reloc(s, segment, addr, 0,
916 R_386_16, false);
917 break;
918 case 4:
919 addr = elf_add_gsym_reloc(s, segment, addr, 0,
920 R_386_32, false);
921 break;
922 default:
923 break;
925 } else if (wrt == elf_plt_sect + 1) {
926 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
927 "relative PLT references");
928 } else {
929 nasm_error(ERR_NONFATAL, "ELF format does not support this"
930 " use of WRT");
931 wrt = NO_SEG; /* we can at least _try_ to continue */
936 if (gnu16) {
937 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
938 "8- or 16-bit relocations in ELF32 is a GNU extension");
939 } else if (asize != 4 && segment != NO_SEG) {
940 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
942 elf_sect_writeaddr(s, addr, asize);
943 break;
946 case OUT_REL1ADR:
947 reltype = R_386_PC8;
948 bytes = 1;
949 goto rel12adr;
950 case OUT_REL2ADR:
951 reltype = R_386_PC16;
952 bytes = 2;
953 goto rel12adr;
955 rel12adr:
956 addr = *(int64_t *)data - size;
957 nasm_assert(segment != segto);
958 if (segment != NO_SEG && segment % 2) {
959 nasm_error(ERR_NONFATAL, "ELF format does not support"
960 " segment base references");
961 } else {
962 if (wrt == NO_SEG) {
963 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
964 "8- or 16-bit relocations in ELF is a GNU extension");
965 elf_add_reloc(s, segment, 0, reltype);
966 } else {
967 nasm_error(ERR_NONFATAL,
968 "Unsupported non-32-bit ELF relocation");
971 elf_sect_writeaddr(s, addr, bytes);
972 break;
974 case OUT_REL4ADR:
975 addr = *(int64_t *)data - size;
976 if (segment == segto)
977 nasm_panic(0, "intra-segment OUT_REL4ADR");
978 if (segment != NO_SEG && segment % 2) {
979 nasm_error(ERR_NONFATAL, "ELF format does not support"
980 " segment base references");
981 } else {
982 if (wrt == NO_SEG) {
983 elf_add_reloc(s, segment, 0, R_386_PC32);
984 } else if (wrt == elf_plt_sect + 1) {
985 elf_add_reloc(s, segment, 0, R_386_PLT32);
986 } else if (wrt == elf_gotpc_sect + 1 ||
987 wrt == elf_gotoff_sect + 1 ||
988 wrt == elf_got_sect + 1) {
989 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
990 "relative GOT references");
991 } else {
992 nasm_error(ERR_NONFATAL, "ELF format does not support this"
993 " use of WRT");
994 wrt = NO_SEG; /* we can at least _try_ to continue */
997 elf_sect_writeaddr(s, addr, 4);
998 break;
1000 case OUT_REL8ADR:
1001 nasm_error(ERR_NONFATAL, "32-bit ELF format does not support 64-bit relocations");
1002 addr = 0;
1003 elf_sect_writeaddr(s, addr, 8);
1004 break;
1006 default:
1007 panic();
1010 static void elf64_out(int32_t segto, const void *data,
1011 enum out_type type, uint64_t size,
1012 int32_t segment, int32_t wrt)
1014 struct elf_section *s;
1015 int64_t addr;
1016 int reltype, bytes;
1017 int i;
1018 static struct symlininfo sinfo;
1021 * handle absolute-assembly (structure definitions)
1023 if (segto == NO_SEG) {
1024 if (type != OUT_RESERVE)
1025 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1026 " space");
1027 return;
1030 s = NULL;
1031 for (i = 0; i < nsects; i++)
1032 if (segto == sects[i]->index) {
1033 s = sects[i];
1034 break;
1036 if (!s) {
1037 int tempint; /* ignored */
1038 if (segto != elf_section_names(".text", 2, &tempint))
1039 nasm_panic(0, "strange segment conditions in ELF driver");
1040 else {
1041 s = sects[nsects - 1];
1042 i = nsects - 1;
1046 /* again some stabs debugging stuff */
1047 sinfo.offset = s->len;
1048 sinfo.section = i;
1049 sinfo.segto = segto;
1050 sinfo.name = s->name;
1051 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1052 /* end of debugging stuff */
1054 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1055 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1056 " BSS section `%s': ignored", s->name);
1057 s->len += realsize(type, size);
1058 return;
1061 switch (type) {
1062 case OUT_RESERVE:
1063 if (s->type == SHT_PROGBITS) {
1064 nasm_error(ERR_WARNING, "uninitialized space declared in"
1065 " non-BSS section `%s': zeroing", s->name);
1066 elf_sect_write(s, NULL, size);
1067 } else
1068 s->len += size;
1069 break;
1071 case OUT_RAWDATA:
1072 if (segment != NO_SEG)
1073 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1074 elf_sect_write(s, data, size);
1075 break;
1077 case OUT_ADDRESS:
1079 int isize = (int)size;
1080 int asize = abs((int)size);
1082 addr = *(int64_t *)data;
1083 if (segment == NO_SEG) {
1084 /* Do nothing */
1085 } else if (segment % 2) {
1086 nasm_error(ERR_NONFATAL, "ELF format does not support"
1087 " segment base references");
1088 } else {
1089 if (wrt == NO_SEG) {
1090 switch (isize) {
1091 case 1:
1092 case -1:
1093 elf_add_reloc(s, segment, addr, R_X86_64_8);
1094 break;
1095 case 2:
1096 case -2:
1097 elf_add_reloc(s, segment, addr, R_X86_64_16);
1098 break;
1099 case 4:
1100 elf_add_reloc(s, segment, addr, R_X86_64_32);
1101 break;
1102 case -4:
1103 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1104 break;
1105 case 8:
1106 case -8:
1107 elf_add_reloc(s, segment, addr, R_X86_64_64);
1108 break;
1109 default:
1110 nasm_panic(0, "internal error elf64-hpa-871");
1111 break;
1113 addr = 0;
1114 } else if (wrt == elf_gotpc_sect + 1) {
1116 * The user will supply GOT relative to $$. ELF
1117 * will let us have GOT relative to $. So we
1118 * need to fix up the data item by $-$$.
1120 addr += s->len;
1121 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1122 addr = 0;
1123 } else if (wrt == elf_gotoff_sect + 1) {
1124 if (asize != 8) {
1125 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
1126 "references to be qword");
1127 } else {
1128 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
1129 addr = 0;
1131 } else if (wrt == elf_got_sect + 1) {
1132 switch (asize) {
1133 case 4:
1134 elf_add_gsym_reloc(s, segment, addr, 0,
1135 R_X86_64_GOT32, true);
1136 addr = 0;
1137 break;
1138 case 8:
1139 elf_add_gsym_reloc(s, segment, addr, 0,
1140 R_X86_64_GOT64, true);
1141 addr = 0;
1142 break;
1143 default:
1144 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1145 break;
1147 } else if (wrt == elf_sym_sect + 1) {
1148 switch (isize) {
1149 case 1:
1150 case -1:
1151 elf_add_gsym_reloc(s, segment, addr, 0,
1152 R_X86_64_8, false);
1153 addr = 0;
1154 break;
1155 case 2:
1156 case -2:
1157 elf_add_gsym_reloc(s, segment, addr, 0,
1158 R_X86_64_16, false);
1159 addr = 0;
1160 break;
1161 case 4:
1162 elf_add_gsym_reloc(s, segment, addr, 0,
1163 R_X86_64_32, false);
1164 addr = 0;
1165 break;
1166 case -4:
1167 elf_add_gsym_reloc(s, segment, addr, 0,
1168 R_X86_64_32S, false);
1169 addr = 0;
1170 break;
1171 case 8:
1172 case -8:
1173 elf_add_gsym_reloc(s, segment, addr, 0,
1174 R_X86_64_64, false);
1175 addr = 0;
1176 break;
1177 default:
1178 nasm_panic(0, "internal error elf64-hpa-903");
1179 break;
1181 } else if (wrt == elf_plt_sect + 1) {
1182 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1183 "relative PLT references");
1184 } else {
1185 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1186 " use of WRT");
1189 elf_sect_writeaddr(s, addr, asize);
1190 break;
1193 case OUT_REL1ADR:
1194 reltype = R_X86_64_PC8;
1195 bytes = 1;
1196 goto rel12adr;
1198 case OUT_REL2ADR:
1199 reltype = R_X86_64_PC16;
1200 bytes = 2;
1201 goto rel12adr;
1203 rel12adr:
1204 addr = *(int64_t *)data - size;
1205 if (segment == segto)
1206 nasm_panic(0, "intra-segment OUT_REL1ADR");
1207 if (segment == NO_SEG) {
1208 /* Do nothing */
1209 } else if (segment % 2) {
1210 nasm_error(ERR_NONFATAL, "ELF format does not support"
1211 " segment base references");
1212 } else {
1213 if (wrt == NO_SEG) {
1214 elf_add_reloc(s, segment, addr, reltype);
1215 addr = 0;
1216 } else {
1217 nasm_error(ERR_NONFATAL,
1218 "Unsupported non-32-bit ELF relocation");
1221 elf_sect_writeaddr(s, addr, bytes);
1222 break;
1224 case OUT_REL4ADR:
1225 addr = *(int64_t *)data - size;
1226 if (segment == segto)
1227 nasm_panic(0, "intra-segment OUT_REL4ADR");
1228 if (segment == NO_SEG) {
1229 /* Do nothing */
1230 } else if (segment % 2) {
1231 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1232 " segment base references");
1233 } else {
1234 if (wrt == NO_SEG) {
1235 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1236 addr = 0;
1237 } else if (wrt == elf_plt_sect + 1) {
1238 elf_add_gsym_reloc(s, segment, addr+size, size,
1239 R_X86_64_PLT32, true);
1240 addr = 0;
1241 } else if (wrt == elf_gotpc_sect + 1 ||
1242 wrt == elf_got_sect + 1) {
1243 elf_add_gsym_reloc(s, segment, addr+size, size,
1244 R_X86_64_GOTPCREL, true);
1245 addr = 0;
1246 } else if (wrt == elf_gotoff_sect + 1 ||
1247 wrt == elf_got_sect + 1) {
1248 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1249 "qword absolute");
1250 } else if (wrt == elf_gottpoff_sect + 1) {
1251 elf_add_gsym_reloc(s, segment, addr+size, size,
1252 R_X86_64_GOTTPOFF, true);
1253 addr = 0;
1254 } else {
1255 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1256 " use of WRT");
1259 elf_sect_writeaddr(s, addr, 4);
1260 break;
1262 case OUT_REL8ADR:
1263 addr = *(int64_t *)data - size;
1264 if (segment == segto)
1265 nasm_panic(0, "intra-segment OUT_REL8ADR");
1266 if (segment == NO_SEG) {
1267 /* Do nothing */
1268 } else if (segment % 2) {
1269 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1270 " segment base references");
1271 } else {
1272 if (wrt == NO_SEG) {
1273 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1274 addr = 0;
1275 } else if (wrt == elf_gotpc_sect + 1 ||
1276 wrt == elf_got_sect + 1) {
1277 elf_add_gsym_reloc(s, segment, addr+size, size,
1278 R_X86_64_GOTPCREL64, true);
1279 addr = 0;
1280 } else if (wrt == elf_gotoff_sect + 1 ||
1281 wrt == elf_got_sect + 1) {
1282 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1283 "absolute");
1284 } else if (wrt == elf_gottpoff_sect + 1) {
1285 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1286 "dword");
1287 } else {
1288 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1289 " use of WRT");
1292 elf_sect_writeaddr(s, addr, 8);
1293 break;
1295 default:
1296 panic();
1300 static void elfx32_out(int32_t segto, const void *data,
1301 enum out_type type, uint64_t size,
1302 int32_t segment, int32_t wrt)
1304 struct elf_section *s;
1305 int64_t addr;
1306 int reltype, bytes;
1307 int i;
1308 static struct symlininfo sinfo;
1311 * handle absolute-assembly (structure definitions)
1313 if (segto == NO_SEG) {
1314 if (type != OUT_RESERVE)
1315 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1316 " space");
1317 return;
1320 s = NULL;
1321 for (i = 0; i < nsects; i++)
1322 if (segto == sects[i]->index) {
1323 s = sects[i];
1324 break;
1326 if (!s) {
1327 int tempint; /* ignored */
1328 if (segto != elf_section_names(".text", 2, &tempint))
1329 nasm_panic(0, "strange segment conditions in ELF driver");
1330 else {
1331 s = sects[nsects - 1];
1332 i = nsects - 1;
1336 /* again some stabs debugging stuff */
1337 sinfo.offset = s->len;
1338 sinfo.section = i;
1339 sinfo.segto = segto;
1340 sinfo.name = s->name;
1341 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1342 /* end of debugging stuff */
1344 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1345 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1346 " BSS section `%s': ignored", s->name);
1347 s->len += realsize(type, size);
1348 return;
1351 switch (type) {
1352 case OUT_RESERVE:
1353 if (s->type == SHT_PROGBITS) {
1354 nasm_error(ERR_WARNING, "uninitialized space declared in"
1355 " non-BSS section `%s': zeroing", s->name);
1356 elf_sect_write(s, NULL, size);
1357 } else
1358 s->len += size;
1359 break;
1361 case OUT_RAWDATA:
1362 if (segment != NO_SEG)
1363 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1364 elf_sect_write(s, data, size);
1365 break;
1367 case OUT_ADDRESS:
1369 int isize = (int)size;
1370 int asize = abs((int)size);
1372 addr = *(int64_t *)data;
1373 if (segment == NO_SEG) {
1374 /* Do nothing */
1375 } else if (segment % 2) {
1376 nasm_error(ERR_NONFATAL, "ELF format does not support"
1377 " segment base references");
1378 } else {
1379 if (wrt == NO_SEG) {
1380 switch (isize) {
1381 case 1:
1382 case -1:
1383 elf_add_reloc(s, segment, addr, R_X86_64_8);
1384 break;
1385 case 2:
1386 case -2:
1387 elf_add_reloc(s, segment, addr, R_X86_64_16);
1388 break;
1389 case 4:
1390 elf_add_reloc(s, segment, addr, R_X86_64_32);
1391 break;
1392 case -4:
1393 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1394 break;
1395 case 8:
1396 case -8:
1397 elf_add_reloc(s, segment, addr, R_X86_64_64);
1398 break;
1399 default:
1400 nasm_panic(0, "internal error elfx32-hpa-871");
1401 break;
1403 addr = 0;
1404 } else if (wrt == elf_gotpc_sect + 1) {
1406 * The user will supply GOT relative to $$. ELF
1407 * will let us have GOT relative to $. So we
1408 * need to fix up the data item by $-$$.
1410 addr += s->len;
1411 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1412 addr = 0;
1413 } else if (wrt == elf_gotoff_sect + 1) {
1414 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
1415 "R_X86_64_GOTOFF64");
1416 } else if (wrt == elf_got_sect + 1) {
1417 switch (asize) {
1418 case 4:
1419 elf_add_gsym_reloc(s, segment, addr, 0,
1420 R_X86_64_GOT32, true);
1421 addr = 0;
1422 break;
1423 default:
1424 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1425 break;
1427 } else if (wrt == elf_sym_sect + 1) {
1428 switch (isize) {
1429 case 1:
1430 case -1:
1431 elf_add_gsym_reloc(s, segment, addr, 0,
1432 R_X86_64_8, false);
1433 addr = 0;
1434 break;
1435 case 2:
1436 case -2:
1437 elf_add_gsym_reloc(s, segment, addr, 0,
1438 R_X86_64_16, false);
1439 addr = 0;
1440 break;
1441 case 4:
1442 elf_add_gsym_reloc(s, segment, addr, 0,
1443 R_X86_64_32, false);
1444 addr = 0;
1445 break;
1446 case -4:
1447 elf_add_gsym_reloc(s, segment, addr, 0,
1448 R_X86_64_32S, false);
1449 addr = 0;
1450 break;
1451 case 8:
1452 case -8:
1453 elf_add_gsym_reloc(s, segment, addr, 0,
1454 R_X86_64_64, false);
1455 addr = 0;
1456 break;
1457 default:
1458 nasm_panic(0, "internal error elfx32-hpa-903");
1459 break;
1461 } else if (wrt == elf_plt_sect + 1) {
1462 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1463 "relative PLT references");
1464 } else {
1465 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1466 " use of WRT");
1469 elf_sect_writeaddr(s, addr, asize);
1470 break;
1473 case OUT_REL1ADR:
1474 reltype = R_X86_64_PC8;
1475 bytes = 1;
1476 goto rel12adr;
1478 case OUT_REL2ADR:
1479 reltype = R_X86_64_PC16;
1480 bytes = 2;
1481 goto rel12adr;
1483 rel12adr:
1484 addr = *(int64_t *)data - size;
1485 if (segment == segto)
1486 nasm_panic(0, "intra-segment OUT_REL1ADR");
1487 if (segment == NO_SEG) {
1488 /* Do nothing */
1489 } else if (segment % 2) {
1490 nasm_error(ERR_NONFATAL, "ELF format does not support"
1491 " segment base references");
1492 } else {
1493 if (wrt == NO_SEG) {
1494 elf_add_reloc(s, segment, addr, reltype);
1495 addr = 0;
1496 } else {
1497 nasm_error(ERR_NONFATAL,
1498 "Unsupported non-32-bit ELF relocation");
1501 elf_sect_writeaddr(s, addr, bytes);
1502 break;
1504 case OUT_REL4ADR:
1505 addr = *(int64_t *)data - size;
1506 if (segment == segto)
1507 nasm_panic(0, "intra-segment OUT_REL4ADR");
1508 if (segment == NO_SEG) {
1509 /* Do nothing */
1510 } else if (segment % 2) {
1511 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
1512 " segment base references");
1513 } else {
1514 if (wrt == NO_SEG) {
1515 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1516 addr = 0;
1517 } else if (wrt == elf_plt_sect + 1) {
1518 elf_add_gsym_reloc(s, segment, addr+size, size,
1519 R_X86_64_PLT32, true);
1520 addr = 0;
1521 } else if (wrt == elf_gotpc_sect + 1 ||
1522 wrt == elf_got_sect + 1) {
1523 elf_add_gsym_reloc(s, segment, addr+size, size,
1524 R_X86_64_GOTPCREL, true);
1525 addr = 0;
1526 } else if (wrt == elf_gotoff_sect + 1 ||
1527 wrt == elf_got_sect + 1) {
1528 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
1529 } else if (wrt == elf_gottpoff_sect + 1) {
1530 elf_add_gsym_reloc(s, segment, addr+size, size,
1531 R_X86_64_GOTTPOFF, true);
1532 addr = 0;
1533 } else {
1534 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
1535 " use of WRT");
1538 elf_sect_writeaddr(s, addr, 4);
1539 break;
1541 case OUT_REL8ADR:
1542 nasm_error(ERR_NONFATAL, "32-bit ELF format does not support 64-bit relocations");
1543 addr = 0;
1544 elf_sect_writeaddr(s, addr, 8);
1545 break;
1547 default:
1548 panic();
1552 static void elf_write(void)
1554 int align;
1555 char *p;
1556 int i;
1558 struct SAA *symtab;
1559 int32_t symtablen, symtablocal;
1562 * Work out how many sections we will have. We have SHN_UNDEF,
1563 * then the flexible user sections, then the fixed sections
1564 * `.shstrtab', `.symtab' and `.strtab', then optionally
1565 * relocation sections for the user sections.
1567 nsections = sec_numspecial + 1;
1568 if (dfmt_is_stabs())
1569 nsections += 3;
1570 else if (dfmt_is_dwarf())
1571 nsections += 10;
1573 add_sectname("", ".shstrtab");
1574 add_sectname("", ".symtab");
1575 add_sectname("", ".strtab");
1576 for (i = 0; i < nsects; i++) {
1577 nsections++; /* for the section itself */
1578 if (sects[i]->head) {
1579 nsections++; /* for its relocations */
1580 add_sectname(is_elf32() ? ".rel" : ".rela", sects[i]->name);
1584 if (dfmt_is_stabs()) {
1585 /* in case the debug information is wanted, just add these three sections... */
1586 add_sectname("", ".stab");
1587 add_sectname("", ".stabstr");
1588 add_sectname(is_elf32() ? ".rel" : ".rela", ".stab");
1589 } else if (dfmt_is_dwarf()) {
1590 /* the dwarf debug standard specifies the following ten sections,
1591 not all of which are currently implemented,
1592 although all of them are defined. */
1593 #define debug_aranges (int64_t) (nsections-10)
1594 #define debug_info (int64_t) (nsections-7)
1595 #define debug_abbrev (int64_t) (nsections-5)
1596 #define debug_line (int64_t) (nsections-4)
1597 add_sectname("", ".debug_aranges");
1598 add_sectname(".rela", ".debug_aranges");
1599 add_sectname("", ".debug_pubnames");
1600 add_sectname("", ".debug_info");
1601 add_sectname(".rela", ".debug_info");
1602 add_sectname("", ".debug_abbrev");
1603 add_sectname("", ".debug_line");
1604 add_sectname(".rela", ".debug_line");
1605 add_sectname("", ".debug_frame");
1606 add_sectname("", ".debug_loc");
1610 * Output the ELF header.
1612 if (is_elf32() || is_elfx32()) {
1613 Elf32_Ehdr ehdr;
1615 nasm_zero(ehdr.e_ident);
1616 memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
1617 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
1618 ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
1619 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1620 ehdr.e_ident[EI_OSABI] = elf_osabi;
1621 ehdr.e_ident[EI_ABIVERSION] = elf_abiver;
1623 ehdr.e_type = cpu_to_le16(ET_REL);
1624 ehdr.e_machine = cpu_to_le16(is_elf32() ? EM_386 : EM_X86_64);
1625 ehdr.e_version = cpu_to_le16(EV_CURRENT);
1626 ehdr.e_entry = 0;
1627 ehdr.e_phoff = 0;
1628 ehdr.e_shoff = sizeof(Elf64_Ehdr);
1629 ehdr.e_flags = 0;
1630 ehdr.e_ehsize = cpu_to_le16(sizeof(Elf32_Ehdr));
1631 ehdr.e_phentsize = 0;
1632 ehdr.e_phnum = 0;
1633 ehdr.e_shentsize = cpu_to_le16(sizeof(Elf32_Shdr));
1634 ehdr.e_shnum = cpu_to_le16(nsections);
1635 ehdr.e_shstrndx = cpu_to_le16(sec_shstrtab);
1637 nasm_write(&ehdr, sizeof(ehdr), ofile);
1638 fwritezero(sizeof(Elf64_Ehdr) - sizeof(Elf32_Ehdr), ofile);
1639 } else {
1640 Elf64_Ehdr ehdr;
1642 nasm_assert(is_elf64());
1644 nasm_zero(ehdr.e_ident);
1645 memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
1646 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
1647 ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
1648 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1649 ehdr.e_ident[EI_OSABI] = elf_osabi;
1650 ehdr.e_ident[EI_ABIVERSION] = elf_abiver;
1652 ehdr.e_type = cpu_to_le16(ET_REL);
1653 ehdr.e_machine = cpu_to_le16(EM_X86_64);
1654 ehdr.e_version = cpu_to_le16(EV_CURRENT);
1655 ehdr.e_entry = 0;
1656 ehdr.e_phoff = 0;
1657 ehdr.e_shoff = sizeof(Elf64_Ehdr);
1658 ehdr.e_flags = 0;
1659 ehdr.e_ehsize = cpu_to_le16(sizeof(Elf64_Ehdr));
1660 ehdr.e_phentsize = 0;
1661 ehdr.e_phnum = 0;
1662 ehdr.e_shentsize = cpu_to_le16(sizeof(Elf64_Shdr));
1663 ehdr.e_shnum = cpu_to_le16(nsections);
1664 ehdr.e_shstrndx = cpu_to_le16(sec_shstrtab);
1666 nasm_write(&ehdr, sizeof(ehdr), ofile);
1670 * Build the symbol table and relocation tables.
1672 symtab = elf_build_symtab(&symtablen, &symtablocal);
1673 for (i = 0; i < nsects; i++)
1674 if (sects[i]->head)
1675 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1676 sects[i]->head);
1679 * Now output the section header table.
1682 elf_foffs = sizeof(Elf64_Ehdr) + (is_elf64() ? sizeof(Elf64_Shdr): sizeof(Elf32_Shdr)) * nsections;
1683 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1684 elf_foffs += align;
1685 elf_nsect = 0;
1686 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1688 /* SHN_UNDEF */
1689 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1690 p = shstrtab + 1;
1692 /* The normal sections */
1693 for (i = 0; i < nsects; i++) {
1694 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1695 (sects[i]->type == SHT_PROGBITS ?
1696 sects[i]->data : NULL), true,
1697 sects[i]->len, 0, 0, sects[i]->align, 0);
1698 p += strlen(p) + 1;
1701 /* .shstrtab */
1702 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1703 shstrtablen, 0, 0, 1, 0);
1704 p += strlen(p) + 1;
1706 /* .symtab */
1707 if (is_elf64())
1708 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1709 symtablen, sec_strtab, symtablocal, 8, 24);
1710 else
1711 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1712 symtablen, sec_strtab, symtablocal, 4, 16);
1713 p += strlen(p) + 1;
1715 /* .strtab */
1716 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1717 strslen, 0, 0, 1, 0);
1718 p += strlen(p) + 1;
1720 /* The relocation sections */
1721 if (is_elf32()) {
1722 for (i = 0; i < nsects; i++) {
1723 if (sects[i]->head) {
1724 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1725 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1726 p += strlen(p) + 1;
1729 } else if (is_elfx32()) {
1730 for (i = 0; i < nsects; i++) {
1731 if (sects[i]->head) {
1732 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1733 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1734 p += strlen(p) + 1;
1737 } else {
1738 nasm_assert(is_elf64());
1739 for (i = 0; i < nsects; i++) {
1740 if (sects[i]->head) {
1741 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1742 sects[i]->rellen, sec_symtab, i + 1, 8, 24);
1743 p += strlen(p) + 1;
1748 if (dfmt_is_stabs()) {
1749 /* for debugging information, create the last three sections
1750 which are the .stab , .stabstr and .rel.stab sections respectively */
1752 /* this function call creates the stab sections in memory */
1753 stabs_generate();
1755 if (stabbuf && stabstrbuf && stabrelbuf) {
1756 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1757 stablen, sec_stabstr, 0, 4, 12);
1758 p += strlen(p) + 1;
1760 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1761 stabstrlen, 0, 0, 4, 0);
1762 p += strlen(p) + 1;
1764 /* link -> symtable info -> section to refer to */
1765 if (is_elf32()) {
1766 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1767 stabrellen, sec_symtab, sec_stab, 4, 8);
1768 } else {
1769 elf_section_header(p - shstrtab, SHT_RELA, 0, stabrelbuf, false,
1770 stabrellen, sec_symtab, sec_stab, 4, is_elf64() ? 24 : 12);
1772 p += strlen(p) + 1;
1774 } else if (dfmt_is_dwarf()) {
1775 /* for dwarf debugging information, create the ten dwarf sections */
1777 /* this function call creates the dwarf sections in memory */
1778 if (dwarf_fsect)
1779 dwarf_generate();
1781 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1782 arangeslen, 0, 0, 1, 0);
1783 p += strlen(p) + 1;
1785 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1786 arangesrellen, sec_symtab,
1787 is_elf64() ? debug_aranges : sec_debug_aranges,
1788 1, is_elf64() ? 24 : 12);
1789 p += strlen(p) + 1;
1791 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1792 false, pubnameslen, 0, 0, 1, 0);
1793 p += strlen(p) + 1;
1795 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1796 infolen, 0, 0, 1, 0);
1797 p += strlen(p) + 1;
1799 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1800 inforellen, sec_symtab,
1801 is_elf64() ? debug_info : sec_debug_info,
1802 1, is_elf64() ? 24 : 12);
1803 p += strlen(p) + 1;
1805 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1806 abbrevlen, 0, 0, 1, 0);
1807 p += strlen(p) + 1;
1809 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1810 linelen, 0, 0, 1, 0);
1811 p += strlen(p) + 1;
1813 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1814 linerellen, sec_symtab,
1815 is_elf64() ? debug_line : sec_debug_line,
1816 1, is_elf64() ? 24 : 12);
1817 p += strlen(p) + 1;
1819 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1820 framelen, 0, 0, 8, 0);
1821 p += strlen(p) + 1;
1823 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1824 loclen, 0, 0, 1, 0);
1825 p += strlen(p) + 1;
1827 fwritezero(align, ofile);
1830 * Now output the sections.
1832 elf_write_sections();
1834 nasm_free(elf_sects);
1835 saa_free(symtab);
1838 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1840 struct SAA *s = saa_init(1L);
1841 struct elf_symbol *sym;
1842 int i;
1844 size_t usize = is_elf64() ? sizeof(Elf64_Sym) : sizeof(Elf32_Sym);
1845 union {
1846 Elf32_Sym sym32;
1847 Elf64_Sym sym64;
1848 } u;
1850 *len = *local = 0;
1853 * Zero symbol first as required by spec.
1855 saa_wbytes(s, NULL, usize);
1856 *len += usize;
1857 (*local)++;
1860 * Next, an entry for the file name.
1862 if (is_elf64()) {
1863 u.sym64.st_name = cpu_to_le32(1);
1864 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_FILE);
1865 u.sym64.st_other = 0;
1866 u.sym64.st_shndx = cpu_to_le16(SHN_ABS);
1867 u.sym64.st_value = 0;
1868 u.sym64.st_size = 0;
1869 } else {
1870 u.sym32.st_name = cpu_to_le32(1);
1871 u.sym32.st_value = 0;
1872 u.sym32.st_size = 0;
1873 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_FILE);
1874 u.sym32.st_other = 0;
1875 u.sym32.st_shndx = cpu_to_le16(SHN_ABS);
1877 saa_wbytes(s, &u, usize);
1878 *len += usize;
1879 (*local)++;
1883 * Now some standard symbols defining the segments, for relocation
1884 * purposes.
1886 if (is_elf64()) {
1887 u.sym64.st_name = 0;
1888 u.sym64.st_other = 0;
1889 u.sym64.st_value = 0;
1890 u.sym64.st_size = 0;
1891 for (i = 1; i <= nsects; i++) {
1892 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_SECTION);
1893 u.sym64.st_shndx = cpu_to_le16(i);
1894 saa_wbytes(s, &u, usize);
1895 *len += usize;
1896 (*local)++;
1898 } else {
1899 u.sym32.st_name = 0;
1900 u.sym32.st_value = 0;
1901 u.sym32.st_size = 0;
1902 u.sym32.st_other = 0;
1903 for (i = 1; i <= nsects; i++) {
1904 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_SECTION);
1905 u.sym32.st_shndx = cpu_to_le16(i);
1906 saa_wbytes(s, &u, usize);
1907 *len += usize;
1908 (*local)++;
1913 * Now the other local symbols.
1915 saa_rewind(syms);
1916 if (is_elf64()) {
1917 while ((sym = saa_rstruct(syms))) {
1918 if (sym->type & SYM_GLOBAL)
1919 continue;
1920 u.sym64.st_name = cpu_to_le32(sym->strpos);
1921 u.sym64.st_info = sym->type;
1922 u.sym64.st_other = sym->other;
1923 u.sym64.st_shndx = cpu_to_le16(sym->section);
1924 u.sym64.st_value = cpu_to_le64(sym->symv.key);
1925 u.sym64.st_size = cpu_to_le64(sym->size);
1926 saa_wbytes(s, &u, usize);
1927 *len += usize;
1928 (*local)++;
1931 * dwarf needs symbols for debug sections
1932 * which are relocation targets.
1934 if (dfmt_is_dwarf()) {
1935 dwarf_infosym = *local;
1936 u.sym64.st_name = 0;
1937 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_SECTION);
1938 u.sym64.st_other = 0;
1939 u.sym64.st_shndx = cpu_to_le16(debug_info);
1940 u.sym64.st_value = 0;
1941 u.sym64.st_size = 0;
1942 saa_wbytes(s, &u, usize);
1943 *len += usize;
1944 (*local)++;
1945 dwarf_abbrevsym = *local;
1946 u.sym64.st_name = 0;
1947 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_SECTION);
1948 u.sym64.st_other = 0;
1949 u.sym64.st_shndx = cpu_to_le16(debug_abbrev);
1950 u.sym64.st_value = 0;
1951 u.sym64.st_size = 0;
1952 saa_wbytes(s, &u, usize);
1953 *len += usize;
1954 (*local)++;
1955 dwarf_linesym = *local;
1956 u.sym64.st_name = 0;
1957 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_SECTION);
1958 u.sym64.st_other = 0;
1959 u.sym64.st_shndx = cpu_to_le16(debug_line);
1960 u.sym64.st_value = 0;
1961 u.sym64.st_size = 0;
1962 saa_wbytes(s, &u, usize);
1963 *len += usize;
1964 (*local)++;
1966 } else {
1967 while ((sym = saa_rstruct(syms))) {
1968 if (sym->type & SYM_GLOBAL)
1969 continue;
1970 u.sym32.st_name = cpu_to_le32(sym->strpos);
1971 u.sym32.st_value = cpu_to_le32(sym->symv.key);
1972 u.sym32.st_size = cpu_to_le32(sym->size);
1973 u.sym32.st_info = sym->type;
1974 u.sym32.st_other = sym->other;
1975 u.sym32.st_shndx = cpu_to_le16(sym->section);
1976 saa_wbytes(s, &u, usize);
1977 *len += usize;
1978 (*local)++;
1981 * dwarf needs symbols for debug sections
1982 * which are relocation targets.
1984 if (dfmt_is_dwarf()) {
1985 dwarf_infosym = *local;
1986 u.sym32.st_name = 0;
1987 u.sym32.st_value = 0;
1988 u.sym32.st_size = 0;
1989 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_SECTION);
1990 u.sym32.st_other = 0;
1991 u.sym32.st_shndx = cpu_to_le16(sec_debug_info);
1992 saa_wbytes(s, &u, usize);
1993 *len += usize;
1994 (*local)++;
1995 dwarf_abbrevsym = *local;
1996 u.sym32.st_name = 0;
1997 u.sym32.st_value = 0;
1998 u.sym32.st_size = 0;
1999 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_SECTION);
2000 u.sym32.st_other = 0;
2001 u.sym32.st_shndx = cpu_to_le16(sec_debug_abbrev);
2002 saa_wbytes(s, &u, usize);
2003 *len += usize;
2004 (*local)++;
2005 dwarf_linesym = *local;
2006 u.sym32.st_name = 0;
2007 u.sym32.st_value = 0;
2008 u.sym32.st_size = 0;
2009 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_SECTION);
2010 u.sym32.st_other = 0;
2011 u.sym32.st_shndx = cpu_to_le16(sec_debug_line);
2012 saa_wbytes(s, &u, usize);
2013 *len += usize;
2014 (*local)++;
2019 * Now the global symbols.
2021 saa_rewind(syms);
2022 if (is_elf64()) {
2023 while ((sym = saa_rstruct(syms))) {
2024 if (!(sym->type & SYM_GLOBAL))
2025 continue;
2026 u.sym64.st_name = cpu_to_le32(sym->strpos);
2027 u.sym64.st_info = sym->type;
2028 u.sym64.st_other = sym->other;
2029 u.sym64.st_shndx = cpu_to_le16(sym->section);
2030 u.sym64.st_value = cpu_to_le64(sym->symv.key);
2031 u.sym64.st_size = cpu_to_le64(sym->size);
2032 saa_wbytes(s, &u, usize);
2033 *len += usize;
2035 } else {
2036 while ((sym = saa_rstruct(syms))) {
2037 if (!(sym->type & SYM_GLOBAL))
2038 continue;
2039 u.sym32.st_name = cpu_to_le32(sym->strpos);
2040 u.sym32.st_value = cpu_to_le32(sym->symv.key);
2041 u.sym32.st_size = cpu_to_le32(sym->size);
2042 u.sym32.st_info = sym->type;
2043 u.sym32.st_other = sym->other;
2044 u.sym32.st_shndx = cpu_to_le16(sym->section);
2045 saa_wbytes(s, &u, usize);
2046 *len += usize;
2050 return s;
2053 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
2055 struct SAA *s;
2056 int32_t global_offset;
2058 size_t usize = is_elf64() ? sizeof(Elf64_Rela) :
2059 (is_elfx32() ? sizeof(Elf32_Rela) : sizeof(Elf32_Rel));
2060 union {
2061 Elf32_Rel rel32;
2062 Elf32_Rela rela32;
2063 Elf64_Rela rela64;
2064 } u;
2066 if (!r)
2067 return NULL;
2069 s = saa_init(1L);
2070 *len = 0;
2073 * How to onvert from a global placeholder to a real symbol index;
2074 * the +2 refers to the two special entries, the null entry and
2075 * the filename entry.
2077 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
2079 if (is_elf32()) {
2080 while (r) {
2081 int32_t sym = r->symbol;
2083 if (sym >= GLOBAL_TEMP_BASE)
2084 sym += global_offset;
2086 u.rel32.r_offset = cpu_to_le32(r->address);
2087 u.rel32.r_info = cpu_to_le32(ELF32_R_INFO(sym, r->type));
2088 saa_wbytes(s, &u, usize);
2089 *len += usize;
2091 r = r->next;
2093 } else if (is_elfx32()) {
2094 while (r) {
2095 int32_t sym = r->symbol;
2097 if (sym >= GLOBAL_TEMP_BASE)
2098 sym += global_offset;
2100 u.rela32.r_offset = cpu_to_le32(r->address);
2101 u.rela32.r_info = cpu_to_le32(ELF32_R_INFO(sym, r->type));
2102 u.rela32.r_addend = cpu_to_le32(r->offset);
2103 saa_wbytes(s, &u, usize);
2104 *len += usize;
2106 r = r->next;
2108 } else {
2109 nasm_assert(is_elf64());
2110 while (r) {
2111 int32_t sym = r->symbol;
2113 if (sym >= GLOBAL_TEMP_BASE)
2114 sym += global_offset;
2116 u.rela64.r_offset = cpu_to_le64(r->address);
2117 u.rela64.r_info = cpu_to_le64(ELF64_R_INFO(sym, r->type));
2118 u.rela64.r_addend = cpu_to_le64(r->offset);
2119 saa_wbytes(s, &u, usize);
2120 *len += usize;
2122 r = r->next;
2126 return s;
2129 static void elf_section_header(int name, int type, uint64_t flags,
2130 void *data, bool is_saa, uint64_t datalen,
2131 int link, int info, int align, int eltsize)
2133 union {
2134 Elf32_Shdr shdr32;
2135 Elf64_Shdr shdr64;
2136 } shdr;
2138 elf_sects[elf_nsect].data = data;
2139 elf_sects[elf_nsect].len = datalen;
2140 elf_sects[elf_nsect].is_saa = is_saa;
2141 elf_nsect++;
2143 if (is_elf32() || is_elfx32()) {
2144 shdr.shdr32.sh_name = cpu_to_le32(name);
2145 shdr.shdr32.sh_type = cpu_to_le32(type);
2146 shdr.shdr32.sh_flags = cpu_to_le32(flags);
2147 shdr.shdr32.sh_addr = 0;
2148 shdr.shdr32.sh_offset = cpu_to_le32(type == SHT_NULL ? 0 : elf_foffs);
2149 shdr.shdr32.sh_size = cpu_to_le32(datalen);
2150 if (data)
2151 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2152 shdr.shdr32.sh_link = cpu_to_le32(link);
2153 shdr.shdr32.sh_info = cpu_to_le32(info);
2154 shdr.shdr32.sh_addralign = cpu_to_le32(align);
2155 shdr.shdr32.sh_entsize = cpu_to_le32(eltsize);
2156 } else {
2157 nasm_assert(is_elf64());
2159 shdr.shdr64.sh_name = cpu_to_le32(name);
2160 shdr.shdr64.sh_type = cpu_to_le32(type);
2161 shdr.shdr64.sh_flags = cpu_to_le64(flags);
2162 shdr.shdr64.sh_addr = 0;
2163 shdr.shdr64.sh_offset = cpu_to_le64(type == SHT_NULL ? 0 : elf_foffs);
2164 shdr.shdr64.sh_size = cpu_to_le32(datalen);
2165 if (data)
2166 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2167 shdr.shdr64.sh_link = cpu_to_le32(link);
2168 shdr.shdr64.sh_info = cpu_to_le32(info);
2169 shdr.shdr64.sh_addralign = cpu_to_le64(align);
2170 shdr.shdr64.sh_entsize = cpu_to_le64(eltsize);
2173 nasm_write(&shdr, is_elf64() ? sizeof(shdr.shdr64) : sizeof(shdr.shdr32), ofile);
2176 static void elf_write_sections(void)
2178 int i;
2179 for (i = 0; i < elf_nsect; i++)
2180 if (elf_sects[i].data) {
2181 int32_t len = elf_sects[i].len;
2182 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
2183 int32_t align = reallen - len;
2184 if (elf_sects[i].is_saa)
2185 saa_fpwrite(elf_sects[i].data, ofile);
2186 else
2187 nasm_write(elf_sects[i].data, len, ofile);
2188 fwritezero(align, ofile);
2192 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
2194 saa_wbytes(sect->data, data, len);
2195 sect->len += len;
2198 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
2200 saa_writeaddr(sect->data, data, len);
2201 sect->len += len;
2204 static void elf_sectalign(int32_t seg, unsigned int value)
2206 struct elf_section *s = NULL;
2207 int i;
2209 for (i = 0; i < nsects; i++) {
2210 if (sects[i]->index == seg) {
2211 s = sects[i];
2212 break;
2215 if (!s || !is_power2(value))
2216 return;
2218 if (value > s->align)
2219 s->align = value;
2222 static int32_t elf_segbase(int32_t segment)
2224 return segment;
2227 extern macros_t elf_stdmac[];
2229 /* Claim "elf" as a pragma namespace, for the future */
2230 static const struct pragma_facility elf_pragma_list[] =
2232 { "elf", NULL },
2233 { NULL, NULL } /* Implements the canonical output name */
2237 static const struct dfmt elf32_df_dwarf = {
2238 "ELF32 (i386) dwarf debug format for Linux/Unix",
2239 "dwarf",
2240 dwarf_init,
2241 dwarf_linenum,
2242 null_debug_deflabel,
2243 null_debug_directive,
2244 debug_typevalue,
2245 dwarf_output,
2246 dwarf_cleanup,
2247 NULL /* pragma list */
2250 static const struct dfmt elf32_df_stabs = {
2251 "ELF32 (i386) stabs debug format for Linux/Unix",
2252 "stabs",
2253 null_debug_init,
2254 stabs_linenum,
2255 null_debug_deflabel,
2256 null_debug_directive,
2257 debug_typevalue,
2258 stabs_output,
2259 stabs_cleanup,
2260 NULL /* pragma list */
2263 static const struct dfmt * const elf32_debugs_arr[3] =
2264 { &elf32_df_dwarf, &elf32_df_stabs, NULL };
2266 const struct ofmt of_elf32 = {
2267 "ELF32 (i386) object files (e.g. Linux)",
2268 "elf32",
2269 ".o",
2272 elf32_debugs_arr,
2273 &elf32_df_stabs,
2274 elf_stdmac,
2275 elf_init,
2276 nasm_do_legacy_output,
2277 elf32_out,
2278 elf_deflabel,
2279 elf_section_names,
2280 elf_sectalign,
2281 elf_segbase,
2282 elf_directive,
2283 elf_cleanup,
2284 elf_pragma_list,
2287 static const struct dfmt elf64_df_dwarf = {
2288 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
2289 "dwarf",
2290 dwarf_init,
2291 dwarf_linenum,
2292 null_debug_deflabel,
2293 null_debug_directive,
2294 debug_typevalue,
2295 dwarf_output,
2296 dwarf_cleanup,
2297 NULL /* pragma list */
2300 static const struct dfmt elf64_df_stabs = {
2301 "ELF64 (x86-64) stabs debug format for Linux/Unix",
2302 "stabs",
2303 null_debug_init,
2304 stabs_linenum,
2305 null_debug_deflabel,
2306 null_debug_directive,
2307 debug_typevalue,
2308 stabs_output,
2309 stabs_cleanup,
2310 NULL /* pragma list */
2313 static const struct dfmt * const elf64_debugs_arr[3] =
2314 { &elf64_df_dwarf, &elf64_df_stabs, NULL };
2316 const struct ofmt of_elf64 = {
2317 "ELF64 (x86_64) object files (e.g. Linux)",
2318 "elf64",
2319 ".o",
2322 elf64_debugs_arr,
2323 &elf64_df_stabs,
2324 elf_stdmac,
2325 elf_init,
2326 nasm_do_legacy_output,
2327 elf64_out,
2328 elf_deflabel,
2329 elf_section_names,
2330 elf_sectalign,
2331 elf_segbase,
2332 elf_directive,
2333 elf_cleanup,
2334 elf_pragma_list,
2337 static const struct dfmt elfx32_df_dwarf = {
2338 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
2339 "dwarf",
2340 dwarf_init,
2341 dwarf_linenum,
2342 null_debug_deflabel,
2343 null_debug_directive,
2344 debug_typevalue,
2345 dwarf_output,
2346 dwarf_cleanup,
2347 NULL /* pragma list */
2350 static const struct dfmt elfx32_df_stabs = {
2351 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
2352 "stabs",
2353 null_debug_init,
2354 stabs_linenum,
2355 null_debug_deflabel,
2356 null_debug_directive,
2357 debug_typevalue,
2358 stabs_output,
2359 stabs_cleanup,
2360 elf_pragma_list,
2363 static const struct dfmt * const elfx32_debugs_arr[3] =
2364 { &elfx32_df_dwarf, &elfx32_df_stabs, NULL };
2366 const struct ofmt of_elfx32 = {
2367 "ELFX32 (x86_64) object files (e.g. Linux)",
2368 "elfx32",
2369 ".o",
2372 elfx32_debugs_arr,
2373 &elfx32_df_stabs,
2374 elf_stdmac,
2375 elf_init,
2376 nasm_do_legacy_output,
2377 elfx32_out,
2378 elf_deflabel,
2379 elf_section_names,
2380 elf_sectalign,
2381 elf_segbase,
2382 elf_directive,
2383 elf_cleanup,
2384 NULL /* pragma list */
2387 static bool is_elf64(void)
2389 return ofmt == &of_elf64;
2392 static bool is_elf32(void)
2394 return ofmt == &of_elf32;
2397 static bool is_elfx32(void)
2399 return ofmt == &of_elfx32;
2402 static bool dfmt_is_stabs(void)
2404 return dfmt == &elf32_df_stabs ||
2405 dfmt == &elfx32_df_stabs ||
2406 dfmt == &elf64_df_stabs;
2409 static bool dfmt_is_dwarf(void)
2411 return dfmt == &elf32_df_dwarf ||
2412 dfmt == &elfx32_df_dwarf ||
2413 dfmt == &elf64_df_dwarf;
2416 /* common debugging routines */
2417 static void debug_typevalue(int32_t type)
2419 int32_t stype, ssize;
2420 switch (TYM_TYPE(type)) {
2421 case TY_LABEL:
2422 ssize = 0;
2423 stype = STT_NOTYPE;
2424 break;
2425 case TY_BYTE:
2426 ssize = 1;
2427 stype = STT_OBJECT;
2428 break;
2429 case TY_WORD:
2430 ssize = 2;
2431 stype = STT_OBJECT;
2432 break;
2433 case TY_DWORD:
2434 ssize = 4;
2435 stype = STT_OBJECT;
2436 break;
2437 case TY_FLOAT:
2438 ssize = 4;
2439 stype = STT_OBJECT;
2440 break;
2441 case TY_QWORD:
2442 ssize = 8;
2443 stype = STT_OBJECT;
2444 break;
2445 case TY_TBYTE:
2446 ssize = 10;
2447 stype = STT_OBJECT;
2448 break;
2449 case TY_OWORD:
2450 ssize = 16;
2451 stype = STT_OBJECT;
2452 break;
2453 case TY_YWORD:
2454 ssize = 32;
2455 stype = STT_OBJECT;
2456 break;
2457 case TY_ZWORD:
2458 ssize = 64;
2459 stype = STT_OBJECT;
2460 break;
2461 case TY_COMMON:
2462 ssize = 0;
2463 stype = STT_COMMON;
2464 break;
2465 case TY_SEG:
2466 ssize = 0;
2467 stype = STT_SECTION;
2468 break;
2469 case TY_EXTERN:
2470 ssize = 0;
2471 stype = STT_NOTYPE;
2472 break;
2473 case TY_EQU:
2474 ssize = 0;
2475 stype = STT_NOTYPE;
2476 break;
2477 default:
2478 ssize = 0;
2479 stype = STT_NOTYPE;
2480 break;
2482 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
2483 lastsym->size = ssize;
2484 lastsym->type = stype;
2488 /* stabs debugging routines */
2490 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t segto)
2492 (void)segto;
2493 if (!stabs_filename) {
2494 stabs_filename = nasm_malloc(strlen(filename) + 1);
2495 strcpy(stabs_filename, filename);
2496 } else {
2497 if (strcmp(stabs_filename, filename)) {
2498 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2499 in fact, this leak comes in quite handy to maintain a list of files
2500 encountered so far in the symbol lines... */
2502 /* why not nasm_free(stabs_filename); we're done with the old one */
2504 stabs_filename = nasm_malloc(strlen(filename) + 1);
2505 strcpy(stabs_filename, filename);
2508 debug_immcall = 1;
2509 currentline = linenumber;
2512 static void stabs_output(int type, void *param)
2514 struct symlininfo *s;
2515 struct linelist *el;
2516 if (type == TY_DEBUGSYMLIN) {
2517 if (debug_immcall) {
2518 s = (struct symlininfo *)param;
2519 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2520 return; /* line info is only collected for executable sections */
2521 numlinestabs++;
2522 el = nasm_malloc(sizeof(struct linelist));
2523 el->info.offset = s->offset;
2524 el->info.section = s->section;
2525 el->info.name = s->name;
2526 el->line = currentline;
2527 el->filename = stabs_filename;
2528 el->next = 0;
2529 if (stabslines) {
2530 stabslines->last->next = el;
2531 stabslines->last = el;
2532 } else {
2533 stabslines = el;
2534 stabslines->last = el;
2538 debug_immcall = 0;
2541 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2543 static void stabs_generate(void)
2545 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
2546 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
2547 char **allfiles;
2548 int *fileidx;
2550 struct linelist *ptr;
2552 ptr = stabslines;
2554 allfiles = nasm_zalloc(numlinestabs * sizeof(char *));
2555 numfiles = 0;
2556 while (ptr) {
2557 if (numfiles == 0) {
2558 allfiles[0] = ptr->filename;
2559 numfiles++;
2560 } else {
2561 for (i = 0; i < numfiles; i++) {
2562 if (!strcmp(allfiles[i], ptr->filename))
2563 break;
2565 if (i >= numfiles) {
2566 allfiles[i] = ptr->filename;
2567 numfiles++;
2570 ptr = ptr->next;
2572 strsize = 1;
2573 fileidx = nasm_malloc(numfiles * sizeof(int));
2574 for (i = 0; i < numfiles; i++) {
2575 fileidx[i] = strsize;
2576 strsize += strlen(allfiles[i]) + 1;
2578 currfile = mainfileindex = 0;
2579 for (i = 0; i < numfiles; i++) {
2580 if (!strcmp(allfiles[i], elf_module)) {
2581 currfile = mainfileindex = i;
2582 break;
2587 * worst case size of the stab buffer would be:
2588 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2589 * plus one "ending" entry
2591 sbuf = nasm_malloc((numlinestabs * 2 + 4) *
2592 sizeof(struct stabentry));
2593 ssbuf = nasm_malloc(strsize);
2594 rbuf = nasm_malloc(numlinestabs * (is_elf64() ? 16 : 8) * (2 + 3));
2595 rptr = rbuf;
2597 for (i = 0; i < numfiles; i++)
2598 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
2599 ssbuf[0] = 0;
2601 stabstrlen = strsize; /* set global variable for length of stab strings */
2603 sptr = sbuf;
2604 ptr = stabslines;
2605 numstabs = 0;
2607 if (ptr) {
2609 * this is the first stab, its strx points to the filename of the
2610 * the source-file, the n_desc field should be set to the number
2611 * of remaining stabs
2613 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
2615 /* this is the stab for the main source file */
2616 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
2618 /* relocation table entry */
2621 * Since the symbol table has two entries before
2622 * the section symbols, the index in the info.section
2623 * member must be adjusted by adding 2
2626 if (is_elf32()) {
2627 WRITELONG(rptr, (sptr - sbuf) - 4);
2628 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2629 } else if (is_elfx32()) {
2630 WRITELONG(rptr, (sptr - sbuf) - 4);
2631 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2632 WRITELONG(rptr, 0);
2633 } else {
2634 nasm_assert(is_elf64());
2635 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2636 WRITELONG(rptr, R_X86_64_32);
2637 WRITELONG(rptr, ptr->info.section + 2);
2638 WRITEDLONG(rptr, 0);
2640 numstabs++;
2643 if (is_elf32()) {
2644 while (ptr) {
2645 if (strcmp(allfiles[currfile], ptr->filename)) {
2646 /* oops file has changed... */
2647 for (i = 0; i < numfiles; i++)
2648 if (!strcmp(allfiles[i], ptr->filename))
2649 break;
2650 currfile = i;
2651 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2652 ptr->info.offset);
2653 numstabs++;
2655 /* relocation table entry */
2656 WRITELONG(rptr, (sptr - sbuf) - 4);
2657 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2660 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2661 numstabs++;
2663 /* relocation table entry */
2664 WRITELONG(rptr, (sptr - sbuf) - 4);
2665 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2667 ptr = ptr->next;
2669 } else if (is_elfx32()) {
2670 while (ptr) {
2671 if (strcmp(allfiles[currfile], ptr->filename)) {
2672 /* oops file has changed... */
2673 for (i = 0; i < numfiles; i++)
2674 if (!strcmp(allfiles[i], ptr->filename))
2675 break;
2676 currfile = i;
2677 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2678 ptr->info.offset);
2679 numstabs++;
2681 /* relocation table entry */
2682 WRITELONG(rptr, (sptr - sbuf) - 4);
2683 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2684 WRITELONG(rptr, ptr->info.offset);
2687 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2688 numstabs++;
2690 /* relocation table entry */
2691 WRITELONG(rptr, (sptr - sbuf) - 4);
2692 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2693 WRITELONG(rptr, ptr->info.offset);
2695 ptr = ptr->next;
2697 } else {
2698 nasm_assert(is_elf64());
2699 while (ptr) {
2700 if (strcmp(allfiles[currfile], ptr->filename)) {
2701 /* oops file has changed... */
2702 for (i = 0; i < numfiles; i++)
2703 if (!strcmp(allfiles[i], ptr->filename))
2704 break;
2705 currfile = i;
2706 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2707 ptr->info.offset);
2708 numstabs++;
2710 /* relocation table entry */
2711 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2712 WRITELONG(rptr, R_X86_64_32);
2713 WRITELONG(rptr, ptr->info.section + 2);
2714 WRITEDLONG(rptr, ptr->info.offset);
2717 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2718 numstabs++;
2720 /* relocation table entry */
2721 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2722 WRITELONG(rptr, R_X86_64_32);
2723 WRITELONG(rptr, ptr->info.section + 2);
2724 WRITEDLONG(rptr, ptr->info.offset);
2726 ptr = ptr->next;
2730 /* this is an "ending" token */
2731 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
2732 numstabs++;
2734 ((struct stabentry *)sbuf)->n_desc = numstabs;
2736 nasm_free(allfiles);
2737 nasm_free(fileidx);
2739 stablen = (sptr - sbuf);
2740 stabrellen = (rptr - rbuf);
2741 stabrelbuf = rbuf;
2742 stabbuf = sbuf;
2743 stabstrbuf = ssbuf;
2746 static void stabs_cleanup(void)
2748 struct linelist *ptr, *del;
2749 if (!stabslines)
2750 return;
2752 ptr = stabslines;
2753 while (ptr) {
2754 del = ptr;
2755 ptr = ptr->next;
2756 nasm_free(del);
2759 nasm_free(stabbuf);
2760 nasm_free(stabrelbuf);
2761 nasm_free(stabstrbuf);
2764 /* dwarf routines */
2766 static void dwarf_init(void)
2768 ndebugs = 3; /* 3 debug symbols */
2771 static void dwarf_linenum(const char *filename, int32_t linenumber,
2772 int32_t segto)
2774 (void)segto;
2775 dwarf_findfile(filename);
2776 debug_immcall = 1;
2777 currentline = linenumber;
2780 /* called from elf_out with type == TY_DEBUGSYMLIN */
2781 static void dwarf_output(int type, void *param)
2783 int ln, aa, inx, maxln, soc;
2784 struct symlininfo *s;
2785 struct SAA *plinep;
2787 (void)type;
2789 s = (struct symlininfo *)param;
2791 /* line number info is only gathered for executable sections */
2792 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2793 return;
2795 /* Check if section index has changed */
2796 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
2797 dwarf_findsect(s->section);
2799 /* do nothing unless line or file has changed */
2800 if (!debug_immcall)
2801 return;
2803 ln = currentline - dwarf_csect->line;
2804 aa = s->offset - dwarf_csect->offset;
2805 inx = dwarf_clist->line;
2806 plinep = dwarf_csect->psaa;
2807 /* check for file change */
2808 if (!(inx == dwarf_csect->file)) {
2809 saa_write8(plinep,DW_LNS_set_file);
2810 saa_write8(plinep,inx);
2811 dwarf_csect->file = inx;
2813 /* check for line change */
2814 if (ln) {
2815 /* test if in range of special op code */
2816 maxln = line_base + line_range;
2817 soc = (ln - line_base) + (line_range * aa) + opcode_base;
2818 if (ln >= line_base && ln < maxln && soc < 256) {
2819 saa_write8(plinep,soc);
2820 } else {
2821 saa_write8(plinep,DW_LNS_advance_line);
2822 saa_wleb128s(plinep,ln);
2823 if (aa) {
2824 saa_write8(plinep,DW_LNS_advance_pc);
2825 saa_wleb128u(plinep,aa);
2827 saa_write8(plinep,DW_LNS_copy);
2829 dwarf_csect->line = currentline;
2830 dwarf_csect->offset = s->offset;
2833 /* show change handled */
2834 debug_immcall = 0;
2838 static void dwarf_generate(void)
2840 uint8_t *pbuf;
2841 int indx;
2842 struct linelist *ftentry;
2843 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2844 struct SAA *parangesrel, *plinesrel, *pinforel;
2845 struct sectlist *psect;
2846 size_t saalen, linepoff, totlen, highaddr;
2848 if (is_elf32()) {
2849 /* write epilogues for each line program range */
2850 /* and build aranges section */
2851 paranges = saa_init(1L);
2852 parangesrel = saa_init(1L);
2853 saa_write16(paranges,2); /* dwarf version */
2854 saa_write32(parangesrel, paranges->datalen+4);
2855 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
2856 saa_write32(parangesrel, 0);
2857 saa_write32(paranges,0); /* offset into info */
2858 saa_write8(paranges,4); /* pointer size */
2859 saa_write8(paranges,0); /* not segmented */
2860 saa_write32(paranges,0); /* padding */
2861 /* iterate though sectlist entries */
2862 psect = dwarf_fsect;
2863 totlen = 0;
2864 highaddr = 0;
2865 for (indx = 0; indx < dwarf_nsections; indx++) {
2866 plinep = psect->psaa;
2867 /* Line Number Program Epilogue */
2868 saa_write8(plinep,2); /* std op 2 */
2869 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2870 saa_write8(plinep,DW_LNS_extended_op);
2871 saa_write8(plinep,1); /* operand length */
2872 saa_write8(plinep,DW_LNE_end_sequence);
2873 totlen += plinep->datalen;
2874 /* range table relocation entry */
2875 saa_write32(parangesrel, paranges->datalen + 4);
2876 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2877 saa_write32(parangesrel, (uint32_t) 0);
2878 /* range table entry */
2879 saa_write32(paranges,0x0000); /* range start */
2880 saa_write32(paranges,sects[psect->section]->len); /* range length */
2881 highaddr += sects[psect->section]->len;
2882 /* done with this entry */
2883 psect = psect->next;
2885 saa_write32(paranges,0); /* null address */
2886 saa_write32(paranges,0); /* null length */
2887 saalen = paranges->datalen;
2888 arangeslen = saalen + 4;
2889 arangesbuf = pbuf = nasm_malloc(arangeslen);
2890 WRITELONG(pbuf,saalen); /* initial length */
2891 saa_rnbytes(paranges, pbuf, saalen);
2892 saa_free(paranges);
2893 } else if (is_elfx32()) {
2894 /* write epilogues for each line program range */
2895 /* and build aranges section */
2896 paranges = saa_init(1L);
2897 parangesrel = saa_init(1L);
2898 saa_write16(paranges,3); /* dwarf version */
2899 saa_write32(parangesrel, paranges->datalen+4);
2900 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
2901 saa_write32(parangesrel, 0);
2902 saa_write32(paranges,0); /* offset into info */
2903 saa_write8(paranges,4); /* pointer size */
2904 saa_write8(paranges,0); /* not segmented */
2905 saa_write32(paranges,0); /* padding */
2906 /* iterate though sectlist entries */
2907 psect = dwarf_fsect;
2908 totlen = 0;
2909 highaddr = 0;
2910 for (indx = 0; indx < dwarf_nsections; indx++) {
2911 plinep = psect->psaa;
2912 /* Line Number Program Epilogue */
2913 saa_write8(plinep,2); /* std op 2 */
2914 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2915 saa_write8(plinep,DW_LNS_extended_op);
2916 saa_write8(plinep,1); /* operand length */
2917 saa_write8(plinep,DW_LNE_end_sequence);
2918 totlen += plinep->datalen;
2919 /* range table relocation entry */
2920 saa_write32(parangesrel, paranges->datalen + 4);
2921 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
2922 saa_write32(parangesrel, (uint32_t) 0);
2923 /* range table entry */
2924 saa_write32(paranges,0x0000); /* range start */
2925 saa_write32(paranges,sects[psect->section]->len); /* range length */
2926 highaddr += sects[psect->section]->len;
2927 /* done with this entry */
2928 psect = psect->next;
2930 saa_write32(paranges,0); /* null address */
2931 saa_write32(paranges,0); /* null length */
2932 saalen = paranges->datalen;
2933 arangeslen = saalen + 4;
2934 arangesbuf = pbuf = nasm_malloc(arangeslen);
2935 WRITELONG(pbuf,saalen); /* initial length */
2936 saa_rnbytes(paranges, pbuf, saalen);
2937 saa_free(paranges);
2938 } else {
2939 nasm_assert(is_elf64());
2940 /* write epilogues for each line program range */
2941 /* and build aranges section */
2942 paranges = saa_init(1L);
2943 parangesrel = saa_init(1L);
2944 saa_write16(paranges,3); /* dwarf version */
2945 saa_write64(parangesrel, paranges->datalen+4);
2946 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2947 saa_write64(parangesrel, 0);
2948 saa_write32(paranges,0); /* offset into info */
2949 saa_write8(paranges,8); /* pointer size */
2950 saa_write8(paranges,0); /* not segmented */
2951 saa_write32(paranges,0); /* padding */
2952 /* iterate though sectlist entries */
2953 psect = dwarf_fsect;
2954 totlen = 0;
2955 highaddr = 0;
2956 for (indx = 0; indx < dwarf_nsections; indx++) {
2957 plinep = psect->psaa;
2958 /* Line Number Program Epilogue */
2959 saa_write8(plinep,2); /* std op 2 */
2960 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2961 saa_write8(plinep,DW_LNS_extended_op);
2962 saa_write8(plinep,1); /* operand length */
2963 saa_write8(plinep,DW_LNE_end_sequence);
2964 totlen += plinep->datalen;
2965 /* range table relocation entry */
2966 saa_write64(parangesrel, paranges->datalen + 4);
2967 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2968 saa_write64(parangesrel, (uint64_t) 0);
2969 /* range table entry */
2970 saa_write64(paranges,0x0000); /* range start */
2971 saa_write64(paranges,sects[psect->section]->len); /* range length */
2972 highaddr += sects[psect->section]->len;
2973 /* done with this entry */
2974 psect = psect->next;
2976 saa_write64(paranges,0); /* null address */
2977 saa_write64(paranges,0); /* null length */
2978 saalen = paranges->datalen;
2979 arangeslen = saalen + 4;
2980 arangesbuf = pbuf = nasm_malloc(arangeslen);
2981 WRITELONG(pbuf,saalen); /* initial length */
2982 saa_rnbytes(paranges, pbuf, saalen);
2983 saa_free(paranges);
2986 /* build rela.aranges section */
2987 arangesrellen = saalen = parangesrel->datalen;
2988 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2989 saa_rnbytes(parangesrel, pbuf, saalen);
2990 saa_free(parangesrel);
2992 /* build pubnames section */
2993 ppubnames = saa_init(1L);
2994 saa_write16(ppubnames,3); /* dwarf version */
2995 saa_write32(ppubnames,0); /* offset into info */
2996 saa_write32(ppubnames,0); /* space used in info */
2997 saa_write32(ppubnames,0); /* end of list */
2998 saalen = ppubnames->datalen;
2999 pubnameslen = saalen + 4;
3000 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
3001 WRITELONG(pbuf,saalen); /* initial length */
3002 saa_rnbytes(ppubnames, pbuf, saalen);
3003 saa_free(ppubnames);
3005 if (is_elf32()) {
3006 /* build info section */
3007 pinfo = saa_init(1L);
3008 pinforel = saa_init(1L);
3009 saa_write16(pinfo,2); /* dwarf version */
3010 saa_write32(pinforel, pinfo->datalen + 4);
3011 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
3012 saa_write32(pinforel, 0);
3013 saa_write32(pinfo,0); /* offset into abbrev */
3014 saa_write8(pinfo,4); /* pointer size */
3015 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3016 saa_write32(pinforel, pinfo->datalen + 4);
3017 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3018 saa_write32(pinforel, 0);
3019 saa_write32(pinfo,0); /* DW_AT_low_pc */
3020 saa_write32(pinforel, pinfo->datalen + 4);
3021 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3022 saa_write32(pinforel, 0);
3023 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
3024 saa_write32(pinforel, pinfo->datalen + 4);
3025 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
3026 saa_write32(pinforel, 0);
3027 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3028 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3029 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3030 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3031 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3032 saa_write32(pinforel, pinfo->datalen + 4);
3033 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3034 saa_write32(pinforel, 0);
3035 saa_write32(pinfo,0); /* DW_AT_low_pc */
3036 saa_write32(pinfo,0); /* DW_AT_frame_base */
3037 saa_write8(pinfo,0); /* end of entries */
3038 saalen = pinfo->datalen;
3039 infolen = saalen + 4;
3040 infobuf = pbuf = nasm_malloc(infolen);
3041 WRITELONG(pbuf,saalen); /* initial length */
3042 saa_rnbytes(pinfo, pbuf, saalen);
3043 saa_free(pinfo);
3044 } else if (is_elfx32()) {
3045 /* build info section */
3046 pinfo = saa_init(1L);
3047 pinforel = saa_init(1L);
3048 saa_write16(pinfo,3); /* dwarf version */
3049 saa_write32(pinforel, pinfo->datalen + 4);
3050 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
3051 saa_write32(pinforel, 0);
3052 saa_write32(pinfo,0); /* offset into abbrev */
3053 saa_write8(pinfo,4); /* pointer size */
3054 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3055 saa_write32(pinforel, pinfo->datalen + 4);
3056 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3057 saa_write32(pinforel, 0);
3058 saa_write32(pinfo,0); /* DW_AT_low_pc */
3059 saa_write32(pinforel, pinfo->datalen + 4);
3060 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3061 saa_write32(pinforel, 0);
3062 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
3063 saa_write32(pinforel, pinfo->datalen + 4);
3064 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
3065 saa_write32(pinforel, 0);
3066 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3067 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3068 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3069 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3070 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3071 saa_write32(pinforel, pinfo->datalen + 4);
3072 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3073 saa_write32(pinforel, 0);
3074 saa_write32(pinfo,0); /* DW_AT_low_pc */
3075 saa_write32(pinfo,0); /* DW_AT_frame_base */
3076 saa_write8(pinfo,0); /* end of entries */
3077 saalen = pinfo->datalen;
3078 infolen = saalen + 4;
3079 infobuf = pbuf = nasm_malloc(infolen);
3080 WRITELONG(pbuf,saalen); /* initial length */
3081 saa_rnbytes(pinfo, pbuf, saalen);
3082 saa_free(pinfo);
3083 } else {
3084 nasm_assert(is_elf64());
3085 /* build info section */
3086 pinfo = saa_init(1L);
3087 pinforel = saa_init(1L);
3088 saa_write16(pinfo,3); /* dwarf version */
3089 saa_write64(pinforel, pinfo->datalen + 4);
3090 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
3091 saa_write64(pinforel, 0);
3092 saa_write32(pinfo,0); /* offset into abbrev */
3093 saa_write8(pinfo,8); /* pointer size */
3094 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3095 saa_write64(pinforel, pinfo->datalen + 4);
3096 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3097 saa_write64(pinforel, 0);
3098 saa_write64(pinfo,0); /* DW_AT_low_pc */
3099 saa_write64(pinforel, pinfo->datalen + 4);
3100 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3101 saa_write64(pinforel, 0);
3102 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
3103 saa_write64(pinforel, pinfo->datalen + 4);
3104 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
3105 saa_write64(pinforel, 0);
3106 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3107 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3108 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3109 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3110 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3111 saa_write64(pinforel, pinfo->datalen + 4);
3112 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3113 saa_write64(pinforel, 0);
3114 saa_write64(pinfo,0); /* DW_AT_low_pc */
3115 saa_write64(pinfo,0); /* DW_AT_frame_base */
3116 saa_write8(pinfo,0); /* end of entries */
3117 saalen = pinfo->datalen;
3118 infolen = saalen + 4;
3119 infobuf = pbuf = nasm_malloc(infolen);
3120 WRITELONG(pbuf,saalen); /* initial length */
3121 saa_rnbytes(pinfo, pbuf, saalen);
3122 saa_free(pinfo);
3125 /* build rela.info section */
3126 inforellen = saalen = pinforel->datalen;
3127 inforelbuf = pbuf = nasm_malloc(inforellen);
3128 saa_rnbytes(pinforel, pbuf, saalen);
3129 saa_free(pinforel);
3131 /* build abbrev section */
3132 pabbrev = saa_init(1L);
3133 saa_write8(pabbrev,1); /* entry number LEB128u */
3134 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
3135 saa_write8(pabbrev,1); /* has children */
3136 /* the following attributes and forms are all LEB128u values */
3137 saa_write8(pabbrev,DW_AT_low_pc);
3138 saa_write8(pabbrev,DW_FORM_addr);
3139 saa_write8(pabbrev,DW_AT_high_pc);
3140 saa_write8(pabbrev,DW_FORM_addr);
3141 saa_write8(pabbrev,DW_AT_stmt_list);
3142 saa_write8(pabbrev,DW_FORM_data4);
3143 saa_write8(pabbrev,DW_AT_name);
3144 saa_write8(pabbrev,DW_FORM_string);
3145 saa_write8(pabbrev,DW_AT_producer);
3146 saa_write8(pabbrev,DW_FORM_string);
3147 saa_write8(pabbrev,DW_AT_language);
3148 saa_write8(pabbrev,DW_FORM_data2);
3149 saa_write16(pabbrev,0); /* end of entry */
3150 /* LEB128u usage same as above */
3151 saa_write8(pabbrev,2); /* entry number */
3152 saa_write8(pabbrev,DW_TAG_subprogram);
3153 saa_write8(pabbrev,0); /* no children */
3154 saa_write8(pabbrev,DW_AT_low_pc);
3155 saa_write8(pabbrev,DW_FORM_addr);
3156 saa_write8(pabbrev,DW_AT_frame_base);
3157 saa_write8(pabbrev,DW_FORM_data4);
3158 saa_write16(pabbrev,0); /* end of entry */
3159 /* Terminal zero entry */
3160 saa_write8(pabbrev,0);
3161 abbrevlen = saalen = pabbrev->datalen;
3162 abbrevbuf = pbuf = nasm_malloc(saalen);
3163 saa_rnbytes(pabbrev, pbuf, saalen);
3164 saa_free(pabbrev);
3166 /* build line section */
3167 /* prolog */
3168 plines = saa_init(1L);
3169 saa_write8(plines,1); /* Minimum Instruction Length */
3170 saa_write8(plines,1); /* Initial value of 'is_stmt' */
3171 saa_write8(plines,line_base); /* Line Base */
3172 saa_write8(plines,line_range); /* Line Range */
3173 saa_write8(plines,opcode_base); /* Opcode Base */
3174 /* standard opcode lengths (# of LEB128u operands) */
3175 saa_write8(plines,0); /* Std opcode 1 length */
3176 saa_write8(plines,1); /* Std opcode 2 length */
3177 saa_write8(plines,1); /* Std opcode 3 length */
3178 saa_write8(plines,1); /* Std opcode 4 length */
3179 saa_write8(plines,1); /* Std opcode 5 length */
3180 saa_write8(plines,0); /* Std opcode 6 length */
3181 saa_write8(plines,0); /* Std opcode 7 length */
3182 saa_write8(plines,0); /* Std opcode 8 length */
3183 saa_write8(plines,1); /* Std opcode 9 length */
3184 saa_write8(plines,0); /* Std opcode 10 length */
3185 saa_write8(plines,0); /* Std opcode 11 length */
3186 saa_write8(plines,1); /* Std opcode 12 length */
3187 /* Directory Table */
3188 saa_write8(plines,0); /* End of table */
3189 /* File Name Table */
3190 ftentry = dwarf_flist;
3191 for (indx = 0; indx < dwarf_numfiles; indx++) {
3192 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
3193 saa_write8(plines,0); /* directory LEB128u */
3194 saa_write8(plines,0); /* time LEB128u */
3195 saa_write8(plines,0); /* size LEB128u */
3196 ftentry = ftentry->next;
3198 saa_write8(plines,0); /* End of table */
3199 linepoff = plines->datalen;
3200 linelen = linepoff + totlen + 10;
3201 linebuf = pbuf = nasm_malloc(linelen);
3202 WRITELONG(pbuf,linelen-4); /* initial length */
3203 WRITESHORT(pbuf,3); /* dwarf version */
3204 WRITELONG(pbuf,linepoff); /* offset to line number program */
3205 /* write line header */
3206 saalen = linepoff;
3207 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
3208 pbuf += linepoff;
3209 saa_free(plines);
3210 /* concatonate line program ranges */
3211 linepoff += 13;
3212 plinesrel = saa_init(1L);
3213 psect = dwarf_fsect;
3214 if (is_elf32()) {
3215 for (indx = 0; indx < dwarf_nsections; indx++) {
3216 saa_write32(plinesrel, linepoff);
3217 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
3218 saa_write32(plinesrel, (uint32_t) 0);
3219 plinep = psect->psaa;
3220 saalen = plinep->datalen;
3221 saa_rnbytes(plinep, pbuf, saalen);
3222 pbuf += saalen;
3223 linepoff += saalen;
3224 saa_free(plinep);
3225 /* done with this entry */
3226 psect = psect->next;
3228 } else if (is_elfx32()) {
3229 for (indx = 0; indx < dwarf_nsections; indx++) {
3230 saa_write32(plinesrel, linepoff);
3231 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
3232 saa_write32(plinesrel, 0);
3233 plinep = psect->psaa;
3234 saalen = plinep->datalen;
3235 saa_rnbytes(plinep, pbuf, saalen);
3236 pbuf += saalen;
3237 linepoff += saalen;
3238 saa_free(plinep);
3239 /* done with this entry */
3240 psect = psect->next;
3242 } else {
3243 nasm_assert(is_elf64());
3244 for (indx = 0; indx < dwarf_nsections; indx++) {
3245 saa_write64(plinesrel, linepoff);
3246 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
3247 saa_write64(plinesrel, (uint64_t) 0);
3248 plinep = psect->psaa;
3249 saalen = plinep->datalen;
3250 saa_rnbytes(plinep, pbuf, saalen);
3251 pbuf += saalen;
3252 linepoff += saalen;
3253 saa_free(plinep);
3254 /* done with this entry */
3255 psect = psect->next;
3259 /* build rela.lines section */
3260 linerellen =saalen = plinesrel->datalen;
3261 linerelbuf = pbuf = nasm_malloc(linerellen);
3262 saa_rnbytes(plinesrel, pbuf, saalen);
3263 saa_free(plinesrel);
3265 /* build frame section */
3266 framelen = 4;
3267 framebuf = pbuf = nasm_malloc(framelen);
3268 WRITELONG(pbuf,framelen-4); /* initial length */
3270 /* build loc section */
3271 loclen = 16;
3272 locbuf = pbuf = nasm_malloc(loclen);
3273 if (is_elf32()) {
3274 WRITELONG(pbuf,0); /* null beginning offset */
3275 WRITELONG(pbuf,0); /* null ending offset */
3276 } else if (is_elfx32()) {
3277 WRITELONG(pbuf,0); /* null beginning offset */
3278 WRITELONG(pbuf,0); /* null ending offset */
3279 } else {
3280 nasm_assert(is_elf64());
3281 WRITEDLONG(pbuf,0); /* null beginning offset */
3282 WRITEDLONG(pbuf,0); /* null ending offset */
3286 static void dwarf_cleanup(void)
3288 nasm_free(arangesbuf);
3289 nasm_free(arangesrelbuf);
3290 nasm_free(pubnamesbuf);
3291 nasm_free(infobuf);
3292 nasm_free(inforelbuf);
3293 nasm_free(abbrevbuf);
3294 nasm_free(linebuf);
3295 nasm_free(linerelbuf);
3296 nasm_free(framebuf);
3297 nasm_free(locbuf);
3300 static void dwarf_findfile(const char * fname)
3302 int finx;
3303 struct linelist *match;
3305 /* return if fname is current file name */
3306 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
3307 return;
3309 /* search for match */
3310 match = 0;
3311 if (dwarf_flist) {
3312 match = dwarf_flist;
3313 for (finx = 0; finx < dwarf_numfiles; finx++) {
3314 if (!(strcmp(fname, match->filename))) {
3315 dwarf_clist = match;
3316 return;
3318 match = match->next;
3322 /* add file name to end of list */
3323 dwarf_clist = nasm_malloc(sizeof(struct linelist));
3324 dwarf_numfiles++;
3325 dwarf_clist->line = dwarf_numfiles;
3326 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
3327 strcpy(dwarf_clist->filename,fname);
3328 dwarf_clist->next = 0;
3329 if (!dwarf_flist) { /* if first entry */
3330 dwarf_flist = dwarf_elist = dwarf_clist;
3331 dwarf_clist->last = 0;
3332 } else { /* chain to previous entry */
3333 dwarf_elist->next = dwarf_clist;
3334 dwarf_elist = dwarf_clist;
3338 static void dwarf_findsect(const int index)
3340 int sinx;
3341 struct sectlist *match;
3342 struct SAA *plinep;
3344 /* return if index is current section index */
3345 if (dwarf_csect && (dwarf_csect->section == index))
3346 return;
3348 /* search for match */
3349 match = 0;
3350 if (dwarf_fsect) {
3351 match = dwarf_fsect;
3352 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
3353 if (match->section == index) {
3354 dwarf_csect = match;
3355 return;
3357 match = match->next;
3361 /* add entry to end of list */
3362 dwarf_csect = nasm_malloc(sizeof(struct sectlist));
3363 dwarf_nsections++;
3364 dwarf_csect->psaa = plinep = saa_init(1L);
3365 dwarf_csect->line = 1;
3366 dwarf_csect->offset = 0;
3367 dwarf_csect->file = 1;
3368 dwarf_csect->section = index;
3369 dwarf_csect->next = 0;
3370 /* set relocatable address at start of line program */
3371 saa_write8(plinep,DW_LNS_extended_op);
3372 saa_write8(plinep,is_elf64() ? 9 : 5); /* operand length */
3373 saa_write8(plinep,DW_LNE_set_address);
3374 if (is_elf64())
3375 saa_write64(plinep,0); /* Start Address */
3376 else
3377 saa_write32(plinep,0); /* Start Address */
3379 if (!dwarf_fsect) { /* if first entry */
3380 dwarf_fsect = dwarf_esect = dwarf_csect;
3381 dwarf_csect->last = 0;
3382 } else { /* chain to previous entry */
3383 dwarf_esect->next = dwarf_csect;
3384 dwarf_esect = dwarf_csect;
3388 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */