backend: move wrapper for legacy output functions to a library routine
[nasm.git] / output / outelf.c
blob92dda89910a55702305c42ec526e3b3d75959699
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2014 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 "saa.h"
46 #include "raa.h"
47 #include "stdscan.h"
48 #include "eval.h"
49 #include "outform.h"
50 #include "outlib.h"
51 #include "rbtree.h"
52 #include "ver.h"
54 #include "dwarf.h"
55 #include "stabs.h"
56 #include "outelf.h"
57 #include "elf.h"
59 #if defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32)
61 #define SECT_DELTA 32
62 static struct elf_section **sects;
63 static int nsects, sectlen;
65 #define SHSTR_DELTA 256
66 static char *shstrtab;
67 static int shstrtablen, shstrtabsize;
69 static struct SAA *syms;
70 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
72 static int32_t def_seg;
74 static struct RAA *bsym;
76 static struct SAA *strs;
77 static uint32_t strslen;
79 static struct elf_symbol *fwds;
81 static char elf_module[FILENAME_MAX];
83 extern const struct ofmt of_elf32;
84 extern const struct ofmt of_elf64;
85 extern const struct ofmt of_elfx32;
87 static struct ELF_SECTDATA {
88 void *data;
89 int64_t len;
90 bool is_saa;
91 } *elf_sects;
93 static int elf_nsect, nsections;
94 static int64_t elf_foffs;
96 static void elf_write(void);
97 static void elf_sect_write(struct elf_section *, const void *, size_t);
98 static void elf_sect_writeaddr(struct elf_section *, int64_t, size_t);
99 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
100 int, int);
101 static void elf_write_sections(void);
102 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
103 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
104 static void add_sectname(char *, char *);
106 struct erel {
107 int offset;
108 int info;
111 struct symlininfo {
112 int offset;
113 int section; /* index into sects[] */
114 int segto; /* internal section number */
115 char *name; /* shallow-copied pointer of section name */
118 struct linelist {
119 struct linelist *next;
120 struct linelist *last;
121 struct symlininfo info;
122 char *filename;
123 int line;
126 struct sectlist {
127 struct SAA *psaa;
128 int section;
129 int line;
130 int offset;
131 int file;
132 struct sectlist *next;
133 struct sectlist *last;
136 /* common debug variables */
137 static int currentline = 1;
138 static int debug_immcall = 0;
140 /* stabs debug variables */
141 static struct linelist *stabslines = 0;
142 static int numlinestabs = 0;
143 static char *stabs_filename = 0;
144 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
145 static int stablen, stabstrlen, stabrellen;
147 /* dwarf debug variables */
148 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
149 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
150 static int dwarf_numfiles = 0, dwarf_nsections;
151 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
152 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
153 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
154 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
155 abbrevlen, linelen, linerellen, framelen, loclen;
156 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
158 static struct elf_symbol *lastsym;
160 /* common debugging routines */
161 static void debug_typevalue(int32_t);
163 /* stabs debugging routines */
164 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t);
165 static void stabs_output(int, void *);
166 static void stabs_generate(void);
167 static void stabs_cleanup(void);
169 /* dwarf debugging routines */
170 static void dwarf_init(void);
171 static void dwarf_linenum(const char *filename, int32_t linenumber, int32_t);
172 static void dwarf_output(int, void *);
173 static void dwarf_generate(void);
174 static void dwarf_cleanup(void);
175 static void dwarf_findfile(const char *);
176 static void dwarf_findsect(const int);
178 static bool is_elf64(void);
179 static bool is_elf32(void);
180 static bool is_elfx32(void);
182 static bool dfmt_is_stabs(void);
183 static bool dfmt_is_dwarf(void);
186 * Special NASM section numbers which are used to define ELF special
187 * symbols.
189 static int32_t elf_gotpc_sect, elf_gotoff_sect;
190 static int32_t elf_got_sect, elf_plt_sect;
191 static int32_t elf_sym_sect, elf_gottpoff_sect, elf_tlsie_sect;
193 uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
194 uint8_t elf_abiver = 0; /* Current ABI version */
196 const struct elf_known_section elf_known_sections[] = {
197 { ".text", SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, 16 },
198 { ".rodata", SHT_PROGBITS, SHF_ALLOC, 4 },
199 { ".lrodata", SHT_PROGBITS, SHF_ALLOC, 4 },
200 { ".data", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4 },
201 { ".ldata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4 },
202 { ".bss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4 },
203 { ".lbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4 },
204 { ".tdata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4 },
205 { ".tbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4 },
206 { ".comment", SHT_PROGBITS, 0, 1 },
207 { NULL, SHT_PROGBITS, SHF_ALLOC, 1 } /* default */
210 /* parse section attributes */
211 void elf_section_attrib(char *name, char *attr, int pass,
212 uint32_t *flags_and, uint32_t *flags_or,
213 uint64_t *align, int *type)
215 char *opt, *val, *next;
217 opt = nasm_skip_spaces(attr);
218 if (!opt || !*opt)
219 return;
221 while ((opt = nasm_opt_val(opt, &val, &next))) {
222 if (!nasm_stricmp(opt, "align")) {
223 if (!val) {
224 nasm_error(ERR_NONFATAL,
225 "section align without value specified");
226 } else {
227 *align = atoi(val);
228 if (*align == 0) {
229 *align = SHA_ANY;
230 } else if (!is_power2(*align)) {
231 nasm_error(ERR_NONFATAL,
232 "section alignment %"PRId64" is not a power of two",
233 *align);
234 *align = SHA_ANY;
237 } else if (!nasm_stricmp(opt, "alloc")) {
238 *flags_and |= SHF_ALLOC;
239 *flags_or |= SHF_ALLOC;
240 } else if (!nasm_stricmp(opt, "noalloc")) {
241 *flags_and |= SHF_ALLOC;
242 *flags_or &= ~SHF_ALLOC;
243 } else if (!nasm_stricmp(opt, "exec")) {
244 *flags_and |= SHF_EXECINSTR;
245 *flags_or |= SHF_EXECINSTR;
246 } else if (!nasm_stricmp(opt, "noexec")) {
247 *flags_and |= SHF_EXECINSTR;
248 *flags_or &= ~SHF_EXECINSTR;
249 } else if (!nasm_stricmp(opt, "write")) {
250 *flags_and |= SHF_WRITE;
251 *flags_or |= SHF_WRITE;
252 } else if (!nasm_stricmp(opt, "tls")) {
253 *flags_and |= SHF_TLS;
254 *flags_or |= SHF_TLS;
255 } else if (!nasm_stricmp(opt, "nowrite")) {
256 *flags_and |= SHF_WRITE;
257 *flags_or &= ~SHF_WRITE;
258 } else if (!nasm_stricmp(opt, "progbits")) {
259 *type = SHT_PROGBITS;
260 } else if (!nasm_stricmp(opt, "nobits")) {
261 *type = SHT_NOBITS;
262 } else if (pass == 1) {
263 nasm_error(ERR_WARNING,
264 "Unknown section attribute '%s' ignored on"
265 " declaration of section `%s'", opt, name);
267 opt = next;
271 int elf_directive(enum directives directive, char *value, int pass)
273 int64_t n;
274 bool err;
275 char *p;
277 switch (directive) {
278 case D_OSABI:
279 if (pass == 2)
280 return 1; /* ignore in pass 2 */
282 n = readnum(value, &err);
283 if (err) {
284 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
285 return 1;
288 if (n < 0 || n > 255) {
289 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
290 return 1;
293 elf_osabi = n;
294 elf_abiver = 0;
296 p = strchr(value,',');
297 if (!p)
298 return 1;
300 n = readnum(p + 1, &err);
301 if (err || n < 0 || n > 255) {
302 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
303 return 1;
306 elf_abiver = n;
307 return 1;
309 default:
310 return 0;
314 static void elf_init(void)
316 sects = NULL;
317 nsects = sectlen = 0;
318 syms = saa_init((int32_t)sizeof(struct elf_symbol));
319 nlocals = nglobs = ndebugs = 0;
320 bsym = raa_init();
321 strs = saa_init(1L);
322 saa_wbytes(strs, "\0", 1L);
323 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
324 strslen = 2 + strlen(elf_module);
325 shstrtab = NULL;
326 shstrtablen = shstrtabsize = 0;;
327 add_sectname("", "");
329 fwds = NULL;
332 * FIXME: tlsie is Elf32 only and
333 * gottpoff is Elfx32|64 only.
336 elf_gotpc_sect = seg_alloc();
337 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
338 elf_gotoff_sect = seg_alloc();
339 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
340 elf_got_sect = seg_alloc();
341 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
342 elf_plt_sect = seg_alloc();
343 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
344 elf_sym_sect = seg_alloc();
345 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
346 elf_gottpoff_sect = seg_alloc();
347 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
348 elf_tlsie_sect = seg_alloc();
349 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
351 def_seg = seg_alloc();
354 static void elf_cleanup(void)
356 struct elf_reloc *r;
357 int i;
359 elf_write();
360 for (i = 0; i < nsects; i++) {
361 if (sects[i]->type != SHT_NOBITS)
362 saa_free(sects[i]->data);
363 if (sects[i]->head)
364 saa_free(sects[i]->rel);
365 while (sects[i]->head) {
366 r = sects[i]->head;
367 sects[i]->head = sects[i]->head->next;
368 nasm_free(r);
371 nasm_free(sects);
372 saa_free(syms);
373 raa_free(bsym);
374 saa_free(strs);
375 dfmt->cleanup();
378 /* add entry to the elf .shstrtab section */
379 static void add_sectname(char *firsthalf, char *secondhalf)
381 int len = strlen(firsthalf) + strlen(secondhalf);
382 while (shstrtablen + len + 1 > shstrtabsize)
383 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
384 strcpy(shstrtab + shstrtablen, firsthalf);
385 strcat(shstrtab + shstrtablen, secondhalf);
386 shstrtablen += len + 1;
389 static int elf_make_section(char *name, int type, int flags, int align)
391 struct elf_section *s;
393 s = nasm_zalloc(sizeof(*s));
395 if (type != SHT_NOBITS)
396 s->data = saa_init(1L);
397 s->tail = &s->head;
398 if (!strcmp(name, ".text"))
399 s->index = def_seg;
400 else
401 s->index = seg_alloc();
402 add_sectname("", name);
404 s->name = nasm_strdup(name);
405 s->type = type;
406 s->flags = flags;
407 s->align = align;
409 if (nsects >= sectlen)
410 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
411 sects[nsects++] = s;
413 return nsects - 1;
416 static int32_t elf_section_names(char *name, int pass, int *bits)
418 char *p;
419 uint32_t flags, flags_and, flags_or;
420 uint64_t align;
421 int type, i;
423 if (!name) {
424 *bits = ofmt->maxbits;
425 return def_seg;
428 p = nasm_skip_word(name);
429 if (*p)
430 *p++ = '\0';
431 flags_and = flags_or = type = align = 0;
433 elf_section_attrib(name, p, pass, &flags_and,
434 &flags_or, &align, &type);
436 if (!strcmp(name, ".shstrtab") ||
437 !strcmp(name, ".symtab") ||
438 !strcmp(name, ".strtab")) {
439 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
440 "name `%s'", name);
441 return NO_SEG;
444 for (i = 0; i < nsects; i++)
445 if (!strcmp(name, sects[i]->name))
446 break;
447 if (i == nsects) {
448 const struct elf_known_section *ks = elf_known_sections;
450 while (ks->name) {
451 if (!strcmp(name, ks->name))
452 break;
453 ks++;
456 type = type ? type : ks->type;
457 align = align ? align : ks->align;
458 flags = (ks->flags & ~flags_and) | flags_or;
460 i = elf_make_section(name, type, flags, align);
461 } else if (pass == 1) {
462 if ((type && sects[i]->type != type)
463 || (align && sects[i]->align != align)
464 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
465 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
466 " redeclaration of section `%s'", name);
469 return sects[i]->index;
472 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
473 int is_global, char *special)
475 int pos = strslen;
476 struct elf_symbol *sym;
477 bool special_used = false;
479 #if defined(DEBUG) && DEBUG>2
480 nasm_error(ERR_DEBUG,
481 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
482 name, segment, offset, is_global, special);
483 #endif
484 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
486 * This is a NASM special symbol. We never allow it into
487 * the ELF symbol table, even if it's a valid one. If it
488 * _isn't_ a valid one, we should barf immediately.
490 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
492 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
493 strcmp(name, "..got") && strcmp(name, "..plt") &&
494 strcmp(name, "..sym") && strcmp(name, "..gottpoff") &&
495 strcmp(name, "..tlsie"))
496 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
497 return;
500 if (is_global == 3) {
501 struct elf_symbol **s;
503 * Fix up a forward-reference symbol size from the first
504 * pass.
506 for (s = &fwds; *s; s = &(*s)->nextfwd)
507 if (!strcmp((*s)->name, name)) {
508 struct tokenval tokval;
509 expr *e;
510 char *p = nasm_skip_spaces(nasm_skip_word(special));
512 stdscan_reset();
513 stdscan_set(p);
514 tokval.t_type = TOKEN_INVALID;
515 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
516 if (e) {
517 if (!is_simple(e))
518 nasm_error(ERR_NONFATAL, "cannot use relocatable"
519 " expression as symbol size");
520 else
521 (*s)->size = reloc_value(e);
525 * Remove it from the list of unresolved sizes.
527 nasm_free((*s)->name);
528 *s = (*s)->nextfwd;
529 return;
531 return; /* it wasn't an important one */
534 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
535 strslen += 1 + strlen(name);
537 lastsym = sym = saa_wstruct(syms);
539 memset(&sym->symv, 0, sizeof(struct rbtree));
541 sym->strpos = pos;
542 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
543 sym->other = STV_DEFAULT;
544 sym->size = 0;
545 if (segment == NO_SEG)
546 sym->section = SHN_ABS;
547 else {
548 int i;
549 sym->section = SHN_UNDEF;
550 if (segment == def_seg) {
551 /* we have to be sure at least text section is there */
552 int tempint;
553 if (segment != elf_section_names(".text", 2, &tempint))
554 nasm_panic(0, "strange segment conditions in ELF driver");
556 for (i = 0; i < nsects; i++) {
557 if (segment == sects[i]->index) {
558 sym->section = i + 1;
559 break;
564 if (is_global == 2) {
565 sym->size = offset;
566 sym->symv.key = 0;
567 sym->section = SHN_COMMON;
569 * We have a common variable. Check the special text to see
570 * if it's a valid number and power of two; if so, store it
571 * as the alignment for the common variable.
573 if (special) {
574 bool err;
575 sym->symv.key = readnum(special, &err);
576 if (err)
577 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
578 " valid number", special);
579 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
580 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
581 " power of two", special);
583 special_used = true;
584 } else
585 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
587 if (sym->type == SYM_GLOBAL) {
589 * If sym->section == SHN_ABS, then the first line of the
590 * else section would cause a core dump, because its a reference
591 * beyond the end of the section array.
592 * This behaviour is exhibited by this code:
593 * GLOBAL crash_nasm
594 * crash_nasm equ 0
595 * To avoid such a crash, such requests are silently discarded.
596 * This may not be the best solution.
598 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
599 bsym = raa_write(bsym, segment, nglobs);
600 } else if (sym->section != SHN_ABS) {
602 * This is a global symbol; so we must add it to the rbtree
603 * of global symbols in its section.
605 * In addition, we check the special text for symbol
606 * type and size information.
608 sects[sym->section-1]->gsyms =
609 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
611 if (special) {
612 int n = strcspn(special, " \t");
614 if (!nasm_strnicmp(special, "function", n))
615 sym->type |= STT_FUNC;
616 else if (!nasm_strnicmp(special, "data", n) ||
617 !nasm_strnicmp(special, "object", n))
618 sym->type |= STT_OBJECT;
619 else if (!nasm_strnicmp(special, "notype", n))
620 sym->type |= STT_NOTYPE;
621 else
622 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
623 n, special);
624 special += n;
626 special = nasm_skip_spaces(special);
627 if (*special) {
628 n = strcspn(special, " \t");
629 if (!nasm_strnicmp(special, "default", n))
630 sym->other = STV_DEFAULT;
631 else if (!nasm_strnicmp(special, "internal", n))
632 sym->other = STV_INTERNAL;
633 else if (!nasm_strnicmp(special, "hidden", n))
634 sym->other = STV_HIDDEN;
635 else if (!nasm_strnicmp(special, "protected", n))
636 sym->other = STV_PROTECTED;
637 else
638 n = 0;
639 special += n;
642 if (*special) {
643 struct tokenval tokval;
644 expr *e;
645 int fwd = 0;
646 char *saveme = stdscan_get();
648 while (special[n] && nasm_isspace(special[n]))
649 n++;
651 * We have a size expression; attempt to
652 * evaluate it.
654 stdscan_reset();
655 stdscan_set(special + n);
656 tokval.t_type = TOKEN_INVALID;
657 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
658 if (fwd) {
659 sym->nextfwd = fwds;
660 fwds = sym;
661 sym->name = nasm_strdup(name);
662 } else if (e) {
663 if (!is_simple(e))
664 nasm_error(ERR_NONFATAL, "cannot use relocatable"
665 " expression as symbol size");
666 else
667 sym->size = reloc_value(e);
669 stdscan_set(saveme);
671 special_used = true;
674 * If TLS segment, mark symbol accordingly.
676 if (sects[sym->section - 1]->flags & SHF_TLS) {
677 sym->type &= 0xf0;
678 sym->type |= STT_TLS;
681 sym->globnum = nglobs;
682 nglobs++;
683 } else
684 nlocals++;
686 if (special && !special_used)
687 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
690 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
691 int64_t offset, int type)
693 struct elf_reloc *r;
695 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
696 sect->tail = &r->next;
698 r->address = sect->len;
699 r->offset = offset;
701 if (segment != NO_SEG) {
702 int i;
703 for (i = 0; i < nsects; i++)
704 if (segment == sects[i]->index)
705 r->symbol = i + 2;
706 if (!r->symbol)
707 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
709 r->type = type;
711 sect->nrelocs++;
715 * This routine deals with ..got and ..sym relocations: the more
716 * complicated kinds. In shared-library writing, some relocations
717 * with respect to global symbols must refer to the precise symbol
718 * rather than referring to an offset from the base of the section
719 * _containing_ the symbol. Such relocations call to this routine,
720 * which searches the symbol list for the symbol in question.
722 * R_386_GOT32 | R_X86_64_GOT32 references require the _exact_ symbol address to be
723 * used; R_386_32 | R_X86_64_32 references can be at an offset from the symbol.
724 * The boolean argument `exact' tells us this.
726 * Return value is the adjusted value of `addr', having become an
727 * offset from the symbol rather than the section. Should always be
728 * zero when returning from an exact call.
730 * Limitation: if you define two symbols at the same place,
731 * confusion will occur.
733 * Inefficiency: we search, currently, using a linked list which
734 * isn't even necessarily sorted.
736 static int64_t elf_add_gsym_reloc(struct elf_section *sect,
737 int32_t segment, uint64_t offset,
738 int64_t pcrel, int type, bool exact)
740 struct elf_reloc *r;
741 struct elf_section *s;
742 struct elf_symbol *sym;
743 struct rbtree *srb;
744 int i;
747 * First look up the segment/offset pair and find a global
748 * symbol corresponding to it. If it's not one of our segments,
749 * then it must be an external symbol, in which case we're fine
750 * doing a normal elf_add_reloc after first sanity-checking
751 * that the offset from the symbol is zero.
753 s = NULL;
754 for (i = 0; i < nsects; i++)
755 if (segment == sects[i]->index) {
756 s = sects[i];
757 break;
760 if (!s) {
761 if (exact && offset)
762 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
763 else
764 elf_add_reloc(sect, segment, offset - pcrel, type);
765 return 0;
768 srb = rb_search(s->gsyms, offset);
769 if (!srb || (exact && srb->key != offset)) {
770 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
771 " for this reference");
772 return 0;
774 sym = container_of(srb, struct elf_symbol, symv);
776 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
777 sect->tail = &r->next;
779 r->next = NULL;
780 r->address = sect->len;
781 r->offset = offset - pcrel - sym->symv.key;
782 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
783 r->type = type;
785 sect->nrelocs++;
786 return r->offset;
789 static void elf32_out(int32_t segto, const void *data,
790 enum out_type type, uint64_t size,
791 int32_t segment, int32_t wrt)
793 struct elf_section *s;
794 int64_t addr;
795 int reltype, bytes;
796 int i;
797 static struct symlininfo sinfo;
800 * handle absolute-assembly (structure definitions)
802 if (segto == NO_SEG) {
803 if (type != OUT_RESERVE)
804 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
805 " space");
806 return;
809 s = NULL;
810 for (i = 0; i < nsects; i++)
811 if (segto == sects[i]->index) {
812 s = sects[i];
813 break;
815 if (!s) {
816 int tempint; /* ignored */
817 if (segto != elf_section_names(".text", 2, &tempint))
818 nasm_panic(0, "strange segment conditions in ELF driver");
819 else {
820 s = sects[nsects - 1];
821 i = nsects - 1;
825 /* again some stabs debugging stuff */
826 sinfo.offset = s->len;
827 sinfo.section = i;
828 sinfo.segto = segto;
829 sinfo.name = s->name;
830 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
831 /* end of debugging stuff */
833 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
834 nasm_error(ERR_WARNING, "attempt to initialize memory in"
835 " BSS section `%s': ignored", s->name);
836 s->len += realsize(type, size);
837 return;
840 switch (type) {
841 case OUT_RESERVE:
842 if (s->type == SHT_PROGBITS) {
843 nasm_error(ERR_WARNING, "uninitialized space declared in"
844 " non-BSS section `%s': zeroing", s->name);
845 elf_sect_write(s, NULL, size);
846 } else
847 s->len += size;
848 break;
850 case OUT_RAWDATA:
851 if (segment != NO_SEG)
852 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
853 elf_sect_write(s, data, size);
854 break;
856 case OUT_ADDRESS:
858 bool gnu16 = false;
859 int asize = abs((int)size);
861 addr = *(int64_t *)data;
862 if (segment != NO_SEG) {
863 if (segment % 2) {
864 nasm_error(ERR_NONFATAL, "ELF format does not support"
865 " segment base references");
866 } else {
867 if (wrt == NO_SEG) {
869 * The if() is a hack to deal with compilers which
870 * don't handle switch() statements with 64-bit
871 * expressions.
873 switch (asize) {
874 case 1:
875 gnu16 = true;
876 elf_add_reloc(s, segment, 0, R_386_8);
877 break;
878 case 2:
879 gnu16 = true;
880 elf_add_reloc(s, segment, 0, R_386_16);
881 break;
882 case 4:
883 elf_add_reloc(s, segment, 0, R_386_32);
884 break;
885 default: /* Error issued further down */
886 break;
888 } else if (wrt == elf_gotpc_sect + 1) {
890 * The user will supply GOT relative to $$. ELF
891 * will let us have GOT relative to $. So we
892 * need to fix up the data item by $-$$.
894 addr += s->len;
895 elf_add_reloc(s, segment, 0, R_386_GOTPC);
896 } else if (wrt == elf_gotoff_sect + 1) {
897 elf_add_reloc(s, segment, 0, R_386_GOTOFF);
898 } else if (wrt == elf_tlsie_sect + 1) {
899 addr = elf_add_gsym_reloc(s, segment, addr, 0,
900 R_386_TLS_IE, true);
901 } else if (wrt == elf_got_sect + 1) {
902 addr = elf_add_gsym_reloc(s, segment, addr, 0,
903 R_386_GOT32, true);
904 } else if (wrt == elf_sym_sect + 1) {
905 switch (asize) {
906 case 1:
907 gnu16 = true;
908 addr = elf_add_gsym_reloc(s, segment, addr, 0,
909 R_386_8, false);
910 break;
911 case 2:
912 gnu16 = true;
913 addr = elf_add_gsym_reloc(s, segment, addr, 0,
914 R_386_16, false);
915 break;
916 case 4:
917 addr = elf_add_gsym_reloc(s, segment, addr, 0,
918 R_386_32, false);
919 break;
920 default:
921 break;
923 } else if (wrt == elf_plt_sect + 1) {
924 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
925 "relative PLT references");
926 } else {
927 nasm_error(ERR_NONFATAL, "ELF format does not support this"
928 " use of WRT");
929 wrt = NO_SEG; /* we can at least _try_ to continue */
934 if (gnu16) {
935 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
936 "8- or 16-bit relocations in ELF32 is a GNU extension");
937 } else if (asize != 4 && segment != NO_SEG) {
938 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
940 elf_sect_writeaddr(s, addr, asize);
941 break;
944 case OUT_REL1ADR:
945 reltype = R_386_PC8;
946 bytes = 1;
947 goto rel12adr;
948 case OUT_REL2ADR:
949 reltype = R_386_PC16;
950 bytes = 2;
951 goto rel12adr;
953 rel12adr:
954 addr = *(int64_t *)data - size;
955 nasm_assert(segment != segto);
956 if (segment != NO_SEG && segment % 2) {
957 nasm_error(ERR_NONFATAL, "ELF format does not support"
958 " segment base references");
959 } else {
960 if (wrt == NO_SEG) {
961 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
962 "8- or 16-bit relocations in ELF is a GNU extension");
963 elf_add_reloc(s, segment, 0, reltype);
964 } else {
965 nasm_error(ERR_NONFATAL,
966 "Unsupported non-32-bit ELF relocation");
969 elf_sect_writeaddr(s, addr, bytes);
970 break;
972 case OUT_REL4ADR:
973 addr = *(int64_t *)data - size;
974 if (segment == segto)
975 nasm_panic(0, "intra-segment OUT_REL4ADR");
976 if (segment != NO_SEG && segment % 2) {
977 nasm_error(ERR_NONFATAL, "ELF format does not support"
978 " segment base references");
979 } else {
980 if (wrt == NO_SEG) {
981 elf_add_reloc(s, segment, 0, R_386_PC32);
982 } else if (wrt == elf_plt_sect + 1) {
983 elf_add_reloc(s, segment, 0, R_386_PLT32);
984 } else if (wrt == elf_gotpc_sect + 1 ||
985 wrt == elf_gotoff_sect + 1 ||
986 wrt == elf_got_sect + 1) {
987 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
988 "relative GOT references");
989 } else {
990 nasm_error(ERR_NONFATAL, "ELF format does not support this"
991 " use of WRT");
992 wrt = NO_SEG; /* we can at least _try_ to continue */
995 elf_sect_writeaddr(s, addr, 4);
996 break;
998 case OUT_REL8ADR:
999 nasm_error(ERR_NONFATAL, "32-bit ELF format does not support 64-bit relocations");
1000 addr = 0;
1001 elf_sect_writeaddr(s, addr, 8);
1002 break;
1004 default:
1005 panic();
1008 static void elf64_out(int32_t segto, const void *data,
1009 enum out_type type, uint64_t size,
1010 int32_t segment, int32_t wrt)
1012 struct elf_section *s;
1013 int64_t addr;
1014 int reltype, bytes;
1015 int i;
1016 static struct symlininfo sinfo;
1019 * handle absolute-assembly (structure definitions)
1021 if (segto == NO_SEG) {
1022 if (type != OUT_RESERVE)
1023 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1024 " space");
1025 return;
1028 s = NULL;
1029 for (i = 0; i < nsects; i++)
1030 if (segto == sects[i]->index) {
1031 s = sects[i];
1032 break;
1034 if (!s) {
1035 int tempint; /* ignored */
1036 if (segto != elf_section_names(".text", 2, &tempint))
1037 nasm_panic(0, "strange segment conditions in ELF driver");
1038 else {
1039 s = sects[nsects - 1];
1040 i = nsects - 1;
1044 /* again some stabs debugging stuff */
1045 sinfo.offset = s->len;
1046 sinfo.section = i;
1047 sinfo.segto = segto;
1048 sinfo.name = s->name;
1049 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1050 /* end of debugging stuff */
1052 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1053 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1054 " BSS section `%s': ignored", s->name);
1055 s->len += realsize(type, size);
1056 return;
1059 switch (type) {
1060 case OUT_RESERVE:
1061 if (s->type == SHT_PROGBITS) {
1062 nasm_error(ERR_WARNING, "uninitialized space declared in"
1063 " non-BSS section `%s': zeroing", s->name);
1064 elf_sect_write(s, NULL, size);
1065 } else
1066 s->len += size;
1067 break;
1069 case OUT_RAWDATA:
1070 if (segment != NO_SEG)
1071 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1072 elf_sect_write(s, data, size);
1073 break;
1075 case OUT_ADDRESS:
1077 int isize = (int)size;
1078 int asize = abs((int)size);
1080 addr = *(int64_t *)data;
1081 if (segment == NO_SEG) {
1082 /* Do nothing */
1083 } else if (segment % 2) {
1084 nasm_error(ERR_NONFATAL, "ELF format does not support"
1085 " segment base references");
1086 } else {
1087 if (wrt == NO_SEG) {
1088 switch (isize) {
1089 case 1:
1090 case -1:
1091 elf_add_reloc(s, segment, addr, R_X86_64_8);
1092 break;
1093 case 2:
1094 case -2:
1095 elf_add_reloc(s, segment, addr, R_X86_64_16);
1096 break;
1097 case 4:
1098 elf_add_reloc(s, segment, addr, R_X86_64_32);
1099 break;
1100 case -4:
1101 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1102 break;
1103 case 8:
1104 case -8:
1105 elf_add_reloc(s, segment, addr, R_X86_64_64);
1106 break;
1107 default:
1108 nasm_panic(0, "internal error elf64-hpa-871");
1109 break;
1111 addr = 0;
1112 } else if (wrt == elf_gotpc_sect + 1) {
1114 * The user will supply GOT relative to $$. ELF
1115 * will let us have GOT relative to $. So we
1116 * need to fix up the data item by $-$$.
1118 addr += s->len;
1119 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1120 addr = 0;
1121 } else if (wrt == elf_gotoff_sect + 1) {
1122 if (asize != 8) {
1123 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
1124 "references to be qword");
1125 } else {
1126 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
1127 addr = 0;
1129 } else if (wrt == elf_got_sect + 1) {
1130 switch (asize) {
1131 case 4:
1132 elf_add_gsym_reloc(s, segment, addr, 0,
1133 R_X86_64_GOT32, true);
1134 addr = 0;
1135 break;
1136 case 8:
1137 elf_add_gsym_reloc(s, segment, addr, 0,
1138 R_X86_64_GOT64, true);
1139 addr = 0;
1140 break;
1141 default:
1142 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1143 break;
1145 } else if (wrt == elf_sym_sect + 1) {
1146 switch (isize) {
1147 case 1:
1148 case -1:
1149 elf_add_gsym_reloc(s, segment, addr, 0,
1150 R_X86_64_8, false);
1151 addr = 0;
1152 break;
1153 case 2:
1154 case -2:
1155 elf_add_gsym_reloc(s, segment, addr, 0,
1156 R_X86_64_16, false);
1157 addr = 0;
1158 break;
1159 case 4:
1160 elf_add_gsym_reloc(s, segment, addr, 0,
1161 R_X86_64_32, false);
1162 addr = 0;
1163 break;
1164 case -4:
1165 elf_add_gsym_reloc(s, segment, addr, 0,
1166 R_X86_64_32S, false);
1167 addr = 0;
1168 break;
1169 case 8:
1170 case -8:
1171 elf_add_gsym_reloc(s, segment, addr, 0,
1172 R_X86_64_64, false);
1173 addr = 0;
1174 break;
1175 default:
1176 nasm_panic(0, "internal error elf64-hpa-903");
1177 break;
1179 } else if (wrt == elf_plt_sect + 1) {
1180 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1181 "relative PLT references");
1182 } else {
1183 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1184 " use of WRT");
1187 elf_sect_writeaddr(s, addr, asize);
1188 break;
1191 case OUT_REL1ADR:
1192 reltype = R_X86_64_PC8;
1193 bytes = 1;
1194 goto rel12adr;
1196 case OUT_REL2ADR:
1197 reltype = R_X86_64_PC16;
1198 bytes = 2;
1199 goto rel12adr;
1201 rel12adr:
1202 addr = *(int64_t *)data - size;
1203 if (segment == segto)
1204 nasm_panic(0, "intra-segment OUT_REL1ADR");
1205 if (segment == NO_SEG) {
1206 /* Do nothing */
1207 } else if (segment % 2) {
1208 nasm_error(ERR_NONFATAL, "ELF format does not support"
1209 " segment base references");
1210 } else {
1211 if (wrt == NO_SEG) {
1212 elf_add_reloc(s, segment, addr, reltype);
1213 addr = 0;
1214 } else {
1215 nasm_error(ERR_NONFATAL,
1216 "Unsupported non-32-bit ELF relocation");
1219 elf_sect_writeaddr(s, addr, bytes);
1220 break;
1222 case OUT_REL4ADR:
1223 addr = *(int64_t *)data - size;
1224 if (segment == segto)
1225 nasm_panic(0, "intra-segment OUT_REL4ADR");
1226 if (segment == NO_SEG) {
1227 /* Do nothing */
1228 } else if (segment % 2) {
1229 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1230 " segment base references");
1231 } else {
1232 if (wrt == NO_SEG) {
1233 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1234 addr = 0;
1235 } else if (wrt == elf_plt_sect + 1) {
1236 elf_add_gsym_reloc(s, segment, addr+size, size,
1237 R_X86_64_PLT32, true);
1238 addr = 0;
1239 } else if (wrt == elf_gotpc_sect + 1 ||
1240 wrt == elf_got_sect + 1) {
1241 elf_add_gsym_reloc(s, segment, addr+size, size,
1242 R_X86_64_GOTPCREL, true);
1243 addr = 0;
1244 } else if (wrt == elf_gotoff_sect + 1 ||
1245 wrt == elf_got_sect + 1) {
1246 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1247 "qword absolute");
1248 } else if (wrt == elf_gottpoff_sect + 1) {
1249 elf_add_gsym_reloc(s, segment, addr+size, size,
1250 R_X86_64_GOTTPOFF, true);
1251 addr = 0;
1252 } else {
1253 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1254 " use of WRT");
1257 elf_sect_writeaddr(s, addr, 4);
1258 break;
1260 case OUT_REL8ADR:
1261 addr = *(int64_t *)data - size;
1262 if (segment == segto)
1263 nasm_panic(0, "intra-segment OUT_REL8ADR");
1264 if (segment == NO_SEG) {
1265 /* Do nothing */
1266 } else if (segment % 2) {
1267 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1268 " segment base references");
1269 } else {
1270 if (wrt == NO_SEG) {
1271 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1272 addr = 0;
1273 } else if (wrt == elf_gotpc_sect + 1 ||
1274 wrt == elf_got_sect + 1) {
1275 elf_add_gsym_reloc(s, segment, addr+size, size,
1276 R_X86_64_GOTPCREL64, true);
1277 addr = 0;
1278 } else if (wrt == elf_gotoff_sect + 1 ||
1279 wrt == elf_got_sect + 1) {
1280 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1281 "absolute");
1282 } else if (wrt == elf_gottpoff_sect + 1) {
1283 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1284 "dword");
1285 } else {
1286 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1287 " use of WRT");
1290 elf_sect_writeaddr(s, addr, 8);
1291 break;
1293 default:
1294 panic();
1298 static void elfx32_out(int32_t segto, const void *data,
1299 enum out_type type, uint64_t size,
1300 int32_t segment, int32_t wrt)
1302 struct elf_section *s;
1303 int64_t addr;
1304 int reltype, bytes;
1305 int i;
1306 static struct symlininfo sinfo;
1309 * handle absolute-assembly (structure definitions)
1311 if (segto == NO_SEG) {
1312 if (type != OUT_RESERVE)
1313 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1314 " space");
1315 return;
1318 s = NULL;
1319 for (i = 0; i < nsects; i++)
1320 if (segto == sects[i]->index) {
1321 s = sects[i];
1322 break;
1324 if (!s) {
1325 int tempint; /* ignored */
1326 if (segto != elf_section_names(".text", 2, &tempint))
1327 nasm_panic(0, "strange segment conditions in ELF driver");
1328 else {
1329 s = sects[nsects - 1];
1330 i = nsects - 1;
1334 /* again some stabs debugging stuff */
1335 sinfo.offset = s->len;
1336 sinfo.section = i;
1337 sinfo.segto = segto;
1338 sinfo.name = s->name;
1339 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1340 /* end of debugging stuff */
1342 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1343 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1344 " BSS section `%s': ignored", s->name);
1345 s->len += realsize(type, size);
1346 return;
1349 switch (type) {
1350 case OUT_RESERVE:
1351 if (s->type == SHT_PROGBITS) {
1352 nasm_error(ERR_WARNING, "uninitialized space declared in"
1353 " non-BSS section `%s': zeroing", s->name);
1354 elf_sect_write(s, NULL, size);
1355 } else
1356 s->len += size;
1357 break;
1359 case OUT_RAWDATA:
1360 if (segment != NO_SEG)
1361 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1362 elf_sect_write(s, data, size);
1363 break;
1365 case OUT_ADDRESS:
1367 int isize = (int)size;
1368 int asize = abs((int)size);
1370 addr = *(int64_t *)data;
1371 if (segment == NO_SEG) {
1372 /* Do nothing */
1373 } else if (segment % 2) {
1374 nasm_error(ERR_NONFATAL, "ELF format does not support"
1375 " segment base references");
1376 } else {
1377 if (wrt == NO_SEG) {
1378 switch (isize) {
1379 case 1:
1380 case -1:
1381 elf_add_reloc(s, segment, addr, R_X86_64_8);
1382 break;
1383 case 2:
1384 case -2:
1385 elf_add_reloc(s, segment, addr, R_X86_64_16);
1386 break;
1387 case 4:
1388 elf_add_reloc(s, segment, addr, R_X86_64_32);
1389 break;
1390 case -4:
1391 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1392 break;
1393 case 8:
1394 case -8:
1395 elf_add_reloc(s, segment, addr, R_X86_64_64);
1396 break;
1397 default:
1398 nasm_panic(0, "internal error elfx32-hpa-871");
1399 break;
1401 addr = 0;
1402 } else if (wrt == elf_gotpc_sect + 1) {
1404 * The user will supply GOT relative to $$. ELF
1405 * will let us have GOT relative to $. So we
1406 * need to fix up the data item by $-$$.
1408 addr += s->len;
1409 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1410 addr = 0;
1411 } else if (wrt == elf_gotoff_sect + 1) {
1412 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
1413 "R_X86_64_GOTOFF64");
1414 } else if (wrt == elf_got_sect + 1) {
1415 switch (asize) {
1416 case 4:
1417 elf_add_gsym_reloc(s, segment, addr, 0,
1418 R_X86_64_GOT32, true);
1419 addr = 0;
1420 break;
1421 default:
1422 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1423 break;
1425 } else if (wrt == elf_sym_sect + 1) {
1426 switch (isize) {
1427 case 1:
1428 case -1:
1429 elf_add_gsym_reloc(s, segment, addr, 0,
1430 R_X86_64_8, false);
1431 addr = 0;
1432 break;
1433 case 2:
1434 case -2:
1435 elf_add_gsym_reloc(s, segment, addr, 0,
1436 R_X86_64_16, false);
1437 addr = 0;
1438 break;
1439 case 4:
1440 elf_add_gsym_reloc(s, segment, addr, 0,
1441 R_X86_64_32, false);
1442 addr = 0;
1443 break;
1444 case -4:
1445 elf_add_gsym_reloc(s, segment, addr, 0,
1446 R_X86_64_32S, false);
1447 addr = 0;
1448 break;
1449 case 8:
1450 case -8:
1451 elf_add_gsym_reloc(s, segment, addr, 0,
1452 R_X86_64_64, false);
1453 addr = 0;
1454 break;
1455 default:
1456 nasm_panic(0, "internal error elfx32-hpa-903");
1457 break;
1459 } else if (wrt == elf_plt_sect + 1) {
1460 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1461 "relative PLT references");
1462 } else {
1463 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1464 " use of WRT");
1467 elf_sect_writeaddr(s, addr, asize);
1468 break;
1471 case OUT_REL1ADR:
1472 reltype = R_X86_64_PC8;
1473 bytes = 1;
1474 goto rel12adr;
1476 case OUT_REL2ADR:
1477 reltype = R_X86_64_PC16;
1478 bytes = 2;
1479 goto rel12adr;
1481 rel12adr:
1482 addr = *(int64_t *)data - size;
1483 if (segment == segto)
1484 nasm_panic(0, "intra-segment OUT_REL1ADR");
1485 if (segment == NO_SEG) {
1486 /* Do nothing */
1487 } else if (segment % 2) {
1488 nasm_error(ERR_NONFATAL, "ELF format does not support"
1489 " segment base references");
1490 } else {
1491 if (wrt == NO_SEG) {
1492 elf_add_reloc(s, segment, addr, reltype);
1493 addr = 0;
1494 } else {
1495 nasm_error(ERR_NONFATAL,
1496 "Unsupported non-32-bit ELF relocation");
1499 elf_sect_writeaddr(s, addr, bytes);
1500 break;
1502 case OUT_REL4ADR:
1503 addr = *(int64_t *)data - size;
1504 if (segment == segto)
1505 nasm_panic(0, "intra-segment OUT_REL4ADR");
1506 if (segment == NO_SEG) {
1507 /* Do nothing */
1508 } else if (segment % 2) {
1509 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
1510 " segment base references");
1511 } else {
1512 if (wrt == NO_SEG) {
1513 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1514 addr = 0;
1515 } else if (wrt == elf_plt_sect + 1) {
1516 elf_add_gsym_reloc(s, segment, addr+size, size,
1517 R_X86_64_PLT32, true);
1518 addr = 0;
1519 } else if (wrt == elf_gotpc_sect + 1 ||
1520 wrt == elf_got_sect + 1) {
1521 elf_add_gsym_reloc(s, segment, addr+size, size,
1522 R_X86_64_GOTPCREL, true);
1523 addr = 0;
1524 } else if (wrt == elf_gotoff_sect + 1 ||
1525 wrt == elf_got_sect + 1) {
1526 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
1527 } else if (wrt == elf_gottpoff_sect + 1) {
1528 elf_add_gsym_reloc(s, segment, addr+size, size,
1529 R_X86_64_GOTTPOFF, true);
1530 addr = 0;
1531 } else {
1532 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
1533 " use of WRT");
1536 elf_sect_writeaddr(s, addr, 4);
1537 break;
1539 case OUT_REL8ADR:
1540 nasm_error(ERR_NONFATAL, "32-bit ELF format does not support 64-bit relocations");
1541 addr = 0;
1542 elf_sect_writeaddr(s, addr, 8);
1543 break;
1545 default:
1546 panic();
1550 static void elf_write(void)
1552 int align;
1553 char *p;
1554 int i;
1556 struct SAA *symtab;
1557 int32_t symtablen, symtablocal;
1560 * Work out how many sections we will have. We have SHN_UNDEF,
1561 * then the flexible user sections, then the fixed sections
1562 * `.shstrtab', `.symtab' and `.strtab', then optionally
1563 * relocation sections for the user sections.
1565 nsections = sec_numspecial + 1;
1566 if (dfmt_is_stabs())
1567 nsections += 3;
1568 else if (dfmt_is_dwarf())
1569 nsections += 10;
1571 add_sectname("", ".shstrtab");
1572 add_sectname("", ".symtab");
1573 add_sectname("", ".strtab");
1574 for (i = 0; i < nsects; i++) {
1575 nsections++; /* for the section itself */
1576 if (sects[i]->head) {
1577 nsections++; /* for its relocations */
1578 add_sectname(is_elf32() ? ".rel" : ".rela", sects[i]->name);
1582 if (dfmt_is_stabs()) {
1583 /* in case the debug information is wanted, just add these three sections... */
1584 add_sectname("", ".stab");
1585 add_sectname("", ".stabstr");
1586 add_sectname(is_elf32() ? ".rel" : ".rela", ".stab");
1587 } else if (dfmt_is_dwarf()) {
1588 /* the dwarf debug standard specifies the following ten sections,
1589 not all of which are currently implemented,
1590 although all of them are defined. */
1591 #define debug_aranges (int64_t) (nsections-10)
1592 #define debug_info (int64_t) (nsections-7)
1593 #define debug_abbrev (int64_t) (nsections-5)
1594 #define debug_line (int64_t) (nsections-4)
1595 add_sectname("", ".debug_aranges");
1596 add_sectname(".rela", ".debug_aranges");
1597 add_sectname("", ".debug_pubnames");
1598 add_sectname("", ".debug_info");
1599 add_sectname(".rela", ".debug_info");
1600 add_sectname("", ".debug_abbrev");
1601 add_sectname("", ".debug_line");
1602 add_sectname(".rela", ".debug_line");
1603 add_sectname("", ".debug_frame");
1604 add_sectname("", ".debug_loc");
1608 * Output the ELF header.
1610 if (is_elf32() || is_elfx32()) {
1611 nasm_write("\177ELF\1\1\1", 7, ofile);
1612 fputc(elf_osabi, ofile);
1613 fputc(elf_abiver, ofile);
1614 fwritezero(7, ofile);
1615 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1616 fwriteint16_t(is_elf32() ? EM_386 : EM_X86_64, ofile); /* processor ID */
1617 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1618 fwriteint32_t(0L, ofile); /* no entry point */
1619 fwriteint32_t(0L, ofile); /* no program header table */
1620 fwriteint32_t(0x40L, ofile); /* section headers straight after ELF header plus alignment */
1621 fwriteint32_t(0L, ofile); /* no special flags */
1622 fwriteint16_t(0x34, ofile); /* size of ELF header */
1623 fwriteint16_t(0, ofile); /* no program header table, again */
1624 fwriteint16_t(0, ofile); /* still no program header table */
1625 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
1626 fwriteint16_t(nsections, ofile); /* number of sections */
1627 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for section header table */
1629 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1630 fwriteint32_t(0L, ofile);
1631 fwriteint32_t(0L, ofile);
1632 } else {
1633 nasm_assert(is_elf64());
1634 nasm_write("\177ELF\2\1\1", 7, ofile);
1635 fputc(elf_osabi, ofile);
1636 fputc(elf_abiver, ofile);
1637 fwritezero(7, ofile);
1638 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1639 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1640 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1641 fwriteint64_t(0L, ofile); /* no entry point */
1642 fwriteint64_t(0L, ofile); /* no program header table */
1643 fwriteint64_t(0x40L, ofile); /* section headers straight after ELF header plus alignment */
1644 fwriteint32_t(0L, ofile); /* no special flags */
1645 fwriteint16_t(0x40, ofile); /* size of ELF header */
1646 fwriteint16_t(0, ofile); /* no program header table, again */
1647 fwriteint16_t(0, ofile); /* still no program header table */
1648 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1649 fwriteint16_t(nsections, ofile); /* number of sections */
1650 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for section header table */
1654 * Build the symbol table and relocation tables.
1656 symtab = elf_build_symtab(&symtablen, &symtablocal);
1657 for (i = 0; i < nsects; i++)
1658 if (sects[i]->head)
1659 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1660 sects[i]->head);
1663 * Now output the section header table.
1666 elf_foffs = 0x40 + (is_elf64() ? sizeof(Elf64_Shdr): sizeof(Elf32_Shdr)) * nsections;
1667 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1668 elf_foffs += align;
1669 elf_nsect = 0;
1670 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1672 /* SHN_UNDEF */
1673 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1674 p = shstrtab + 1;
1676 /* The normal sections */
1677 for (i = 0; i < nsects; i++) {
1678 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1679 (sects[i]->type == SHT_PROGBITS ?
1680 sects[i]->data : NULL), true,
1681 sects[i]->len, 0, 0, sects[i]->align, 0);
1682 p += strlen(p) + 1;
1685 /* .shstrtab */
1686 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1687 shstrtablen, 0, 0, 1, 0);
1688 p += strlen(p) + 1;
1690 /* .symtab */
1691 if (is_elf64())
1692 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1693 symtablen, sec_strtab, symtablocal, 8, 24);
1694 else
1695 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1696 symtablen, sec_strtab, symtablocal, 4, 16);
1697 p += strlen(p) + 1;
1699 /* .strtab */
1700 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1701 strslen, 0, 0, 1, 0);
1702 p += strlen(p) + 1;
1704 /* The relocation sections */
1705 if (is_elf32()) {
1706 for (i = 0; i < nsects; i++) {
1707 if (sects[i]->head) {
1708 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1709 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1710 p += strlen(p) + 1;
1713 } else if (is_elfx32()) {
1714 for (i = 0; i < nsects; i++) {
1715 if (sects[i]->head) {
1716 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1717 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1718 p += strlen(p) + 1;
1721 } else {
1722 nasm_assert(is_elf64());
1723 for (i = 0; i < nsects; i++) {
1724 if (sects[i]->head) {
1725 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1726 sects[i]->rellen, sec_symtab, i + 1, 8, 24);
1727 p += strlen(p) + 1;
1732 if (dfmt_is_stabs()) {
1733 /* for debugging information, create the last three sections
1734 which are the .stab , .stabstr and .rel.stab sections respectively */
1736 /* this function call creates the stab sections in memory */
1737 stabs_generate();
1739 if (stabbuf && stabstrbuf && stabrelbuf) {
1740 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1741 stablen, sec_stabstr, 0, 4, 12);
1742 p += strlen(p) + 1;
1744 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1745 stabstrlen, 0, 0, 4, 0);
1746 p += strlen(p) + 1;
1748 /* link -> symtable info -> section to refer to */
1749 if (is_elf32()) {
1750 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1751 stabrellen, sec_symtab, sec_stab, 4, 8);
1752 } else {
1753 elf_section_header(p - shstrtab, SHT_RELA, 0, stabrelbuf, false,
1754 stabrellen, sec_symtab, sec_stab, 4, is_elf64() ? 24 : 12);
1756 p += strlen(p) + 1;
1758 } else if (dfmt_is_dwarf()) {
1759 /* for dwarf debugging information, create the ten dwarf sections */
1761 /* this function call creates the dwarf sections in memory */
1762 if (dwarf_fsect)
1763 dwarf_generate();
1765 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1766 arangeslen, 0, 0, 1, 0);
1767 p += strlen(p) + 1;
1769 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1770 arangesrellen, sec_symtab,
1771 is_elf64() ? debug_aranges : sec_debug_aranges,
1772 1, is_elf64() ? 24 : 12);
1773 p += strlen(p) + 1;
1775 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1776 false, pubnameslen, 0, 0, 1, 0);
1777 p += strlen(p) + 1;
1779 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1780 infolen, 0, 0, 1, 0);
1781 p += strlen(p) + 1;
1783 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1784 inforellen, sec_symtab,
1785 is_elf64() ? debug_info : sec_debug_info,
1786 1, is_elf64() ? 24 : 12);
1787 p += strlen(p) + 1;
1789 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1790 abbrevlen, 0, 0, 1, 0);
1791 p += strlen(p) + 1;
1793 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1794 linelen, 0, 0, 1, 0);
1795 p += strlen(p) + 1;
1797 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1798 linerellen, sec_symtab,
1799 is_elf64() ? debug_line : sec_debug_line,
1800 1, is_elf64() ? 24 : 12);
1801 p += strlen(p) + 1;
1803 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1804 framelen, 0, 0, 8, 0);
1805 p += strlen(p) + 1;
1807 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1808 loclen, 0, 0, 1, 0);
1809 p += strlen(p) + 1;
1811 fwritezero(align, ofile);
1814 * Now output the sections.
1816 elf_write_sections();
1818 nasm_free(elf_sects);
1819 saa_free(symtab);
1822 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1824 struct SAA *s = saa_init(1L);
1825 struct elf_symbol *sym;
1826 uint8_t entry[24], *p;
1827 int i;
1829 *len = *local = 0;
1832 * First, an all-zeros entry, required by the ELF spec.
1834 saa_wbytes(s, NULL, is_elf64() ? 24L : 16L); /* null symbol table entry */
1835 *len += is_elf64() ? 24L : 16L;
1836 (*local)++;
1839 * Next, an entry for the file name.
1841 p = entry;
1842 if (is_elf64()) {
1843 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1844 WRITESHORT(p, STT_FILE); /* type FILE */
1845 WRITESHORT(p, SHN_ABS);
1846 WRITEDLONG(p, (uint64_t) 0); /* no value */
1847 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1848 saa_wbytes(s, entry, 24L);
1849 *len += 24;
1850 (*local)++;
1851 } else {
1852 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1853 WRITELONG(p, 0); /* no value */
1854 WRITELONG(p, 0); /* no size either */
1855 WRITESHORT(p, STT_FILE); /* type FILE */
1856 WRITESHORT(p, SHN_ABS);
1857 saa_wbytes(s, entry, 16L);
1858 *len += 16;
1859 (*local)++;
1863 * Now some standard symbols defining the segments, for relocation
1864 * purposes.
1866 if (is_elf64()) {
1867 for (i = 1; i <= nsects; i++) {
1868 p = entry;
1869 WRITELONG(p, 0); /* no symbol name */
1870 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1871 WRITESHORT(p, i); /* section id */
1872 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1873 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1874 saa_wbytes(s, entry, 24L);
1875 *len += 24;
1876 (*local)++;
1878 } else {
1879 for (i = 1; i <= nsects; i++) {
1880 p = entry;
1881 WRITELONG(p, 0); /* no symbol name */
1882 WRITELONG(p, 0); /* offset zero */
1883 WRITELONG(p, 0); /* size zero */
1884 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1885 WRITESHORT(p, i); /* section id */
1886 saa_wbytes(s, entry, 16L);
1887 *len += 16;
1888 (*local)++;
1893 * Now the other local symbols.
1895 saa_rewind(syms);
1896 if (is_elf64()) {
1897 while ((sym = saa_rstruct(syms))) {
1898 if (sym->type & SYM_GLOBAL)
1899 continue;
1900 p = entry;
1901 WRITELONG(p, sym->strpos); /* index into symbol string table */
1902 WRITECHAR(p, sym->type); /* type and binding */
1903 WRITECHAR(p, sym->other); /* visibility */
1904 WRITESHORT(p, sym->section); /* index into section header table */
1905 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1906 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1907 saa_wbytes(s, entry, 24L);
1908 *len += 24;
1909 (*local)++;
1912 * dwarf needs symbols for debug sections
1913 * which are relocation targets.
1915 if (dfmt_is_dwarf()) {
1916 dwarf_infosym = *local;
1917 p = entry;
1918 WRITELONG(p, 0); /* no symbol name */
1919 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1920 WRITESHORT(p, debug_info); /* section id */
1921 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1922 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1923 saa_wbytes(s, entry, 24L);
1924 *len += 24;
1925 (*local)++;
1926 dwarf_abbrevsym = *local;
1927 p = entry;
1928 WRITELONG(p, 0); /* no symbol name */
1929 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1930 WRITESHORT(p, debug_abbrev); /* section id */
1931 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1932 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1933 saa_wbytes(s, entry, 24L);
1934 *len += 24;
1935 (*local)++;
1936 dwarf_linesym = *local;
1937 p = entry;
1938 WRITELONG(p, 0); /* no symbol name */
1939 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1940 WRITESHORT(p, debug_line); /* section id */
1941 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1942 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1943 saa_wbytes(s, entry, 24L);
1944 *len += 24;
1945 (*local)++;
1947 } else {
1948 while ((sym = saa_rstruct(syms))) {
1949 if (sym->type & SYM_GLOBAL)
1950 continue;
1951 p = entry;
1952 WRITELONG(p, sym->strpos);
1953 WRITELONG(p, sym->symv.key);
1954 WRITELONG(p, sym->size);
1955 WRITECHAR(p, sym->type); /* type and binding */
1956 WRITECHAR(p, sym->other); /* visibility */
1957 WRITESHORT(p, sym->section);
1958 saa_wbytes(s, entry, 16L);
1959 *len += 16;
1960 (*local)++;
1963 * dwarf needs symbols for debug sections
1964 * which are relocation targets.
1966 if (dfmt_is_dwarf()) {
1967 dwarf_infosym = *local;
1968 p = entry;
1969 WRITELONG(p, 0); /* no symbol name */
1970 WRITELONG(p, (uint32_t) 0); /* offset zero */
1971 WRITELONG(p, (uint32_t) 0); /* size zero */
1972 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1973 WRITESHORT(p, sec_debug_info); /* section id */
1974 saa_wbytes(s, entry, 16L);
1975 *len += 16;
1976 (*local)++;
1977 dwarf_abbrevsym = *local;
1978 p = entry;
1979 WRITELONG(p, 0); /* no symbol name */
1980 WRITELONG(p, (uint32_t) 0); /* offset zero */
1981 WRITELONG(p, (uint32_t) 0); /* size zero */
1982 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1983 WRITESHORT(p, sec_debug_abbrev); /* section id */
1984 saa_wbytes(s, entry, 16L);
1985 *len += 16;
1986 (*local)++;
1987 dwarf_linesym = *local;
1988 p = entry;
1989 WRITELONG(p, 0); /* no symbol name */
1990 WRITELONG(p, (uint32_t) 0); /* offset zero */
1991 WRITELONG(p, (uint32_t) 0); /* size zero */
1992 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1993 WRITESHORT(p, sec_debug_line); /* section id */
1994 saa_wbytes(s, entry, 16L);
1995 *len += 16;
1996 (*local)++;
2001 * Now the global symbols.
2003 saa_rewind(syms);
2004 if (is_elf64()) {
2005 while ((sym = saa_rstruct(syms))) {
2006 if (!(sym->type & SYM_GLOBAL))
2007 continue;
2008 p = entry;
2009 WRITELONG(p, sym->strpos);
2010 WRITECHAR(p, sym->type); /* type and binding */
2011 WRITECHAR(p, sym->other); /* visibility */
2012 WRITESHORT(p, sym->section);
2013 WRITEDLONG(p, (int64_t)sym->symv.key);
2014 WRITEDLONG(p, (int64_t)sym->size);
2015 saa_wbytes(s, entry, 24L);
2016 *len += 24;
2018 } else {
2019 while ((sym = saa_rstruct(syms))) {
2020 if (!(sym->type & SYM_GLOBAL))
2021 continue;
2022 p = entry;
2023 WRITELONG(p, sym->strpos);
2024 WRITELONG(p, sym->symv.key);
2025 WRITELONG(p, sym->size);
2026 WRITECHAR(p, sym->type); /* type and binding */
2027 WRITECHAR(p, sym->other); /* visibility */
2028 WRITESHORT(p, sym->section);
2029 saa_wbytes(s, entry, 16L);
2030 *len += 16;
2034 return s;
2037 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
2039 struct SAA *s;
2040 uint8_t *p, entry[24];
2041 int32_t global_offset;
2043 if (!r)
2044 return NULL;
2046 s = saa_init(1L);
2047 *len = 0;
2050 * How to onvert from a global placeholder to a real symbol index;
2051 * the +2 refers to the two special entries, the null entry and
2052 * the filename entry.
2054 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
2056 if (is_elf32()) {
2057 while (r) {
2058 int32_t sym = r->symbol;
2060 if (sym >= GLOBAL_TEMP_BASE)
2061 sym += global_offset;
2063 p = entry;
2064 WRITELONG(p, r->address);
2065 WRITELONG(p, (sym << 8) + r->type);
2066 saa_wbytes(s, entry, 8L);
2067 *len += 8;
2069 r = r->next;
2071 } else if (is_elfx32()) {
2072 while (r) {
2073 int32_t sym = r->symbol;
2075 if (sym >= GLOBAL_TEMP_BASE)
2076 sym += global_offset;
2078 p = entry;
2079 WRITELONG(p, r->address);
2080 WRITELONG(p, (sym << 8) + r->type);
2081 WRITELONG(p, r->offset);
2082 saa_wbytes(s, entry, 12L);
2083 *len += 12;
2085 r = r->next;
2087 } else {
2088 nasm_assert(is_elf64());
2089 while (r) {
2090 int32_t sym = r->symbol;
2092 if (sym >= GLOBAL_TEMP_BASE)
2093 sym += global_offset;
2095 p = entry;
2096 WRITEDLONG(p, r->address);
2097 WRITELONG(p, r->type);
2098 WRITELONG(p, sym);
2099 WRITEDLONG(p, r->offset);
2100 saa_wbytes(s, entry, 24L);
2101 *len += 24;
2103 r = r->next;
2107 return s;
2110 static void elf_section_header(int name, int type, uint64_t flags,
2111 void *data, bool is_saa, uint64_t datalen,
2112 int link, int info, int align, int eltsize)
2114 elf_sects[elf_nsect].data = data;
2115 elf_sects[elf_nsect].len = datalen;
2116 elf_sects[elf_nsect].is_saa = is_saa;
2117 elf_nsect++;
2119 if (is_elf32()) {
2120 fwriteint32_t((int32_t)name, ofile);
2121 fwriteint32_t((int32_t)type, ofile);
2122 fwriteint32_t((int32_t)flags, ofile);
2123 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
2124 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
2125 fwriteint32_t(datalen, ofile);
2126 if (data)
2127 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2128 fwriteint32_t((int32_t)link, ofile);
2129 fwriteint32_t((int32_t)info, ofile);
2130 fwriteint32_t((int32_t)align, ofile);
2131 fwriteint32_t((int32_t)eltsize, ofile);
2132 } else if (is_elfx32()) {
2133 fwriteint32_t((int32_t)name, ofile);
2134 fwriteint32_t((int32_t)type, ofile);
2135 fwriteint32_t((int32_t)flags, ofile);
2136 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
2137 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
2138 fwriteint32_t(datalen, ofile);
2139 if (data)
2140 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2141 fwriteint32_t((int32_t)link, ofile);
2142 fwriteint32_t((int32_t)info, ofile);
2143 fwriteint32_t((int32_t)align, ofile);
2144 fwriteint32_t((int32_t)eltsize, ofile);
2145 } else {
2146 nasm_assert(is_elf64());
2147 fwriteint32_t((int32_t)name, ofile);
2148 fwriteint32_t((int32_t)type, ofile);
2149 fwriteint64_t((int64_t)flags, ofile);
2150 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
2151 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
2152 fwriteint64_t(datalen, ofile);
2153 if (data)
2154 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2155 fwriteint32_t((int32_t)link, ofile);
2156 fwriteint32_t((int32_t)info, ofile);
2157 fwriteint64_t((int64_t)align, ofile);
2158 fwriteint64_t((int64_t)eltsize, ofile);
2162 static void elf_write_sections(void)
2164 int i;
2165 for (i = 0; i < elf_nsect; i++)
2166 if (elf_sects[i].data) {
2167 int32_t len = elf_sects[i].len;
2168 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
2169 int32_t align = reallen - len;
2170 if (elf_sects[i].is_saa)
2171 saa_fpwrite(elf_sects[i].data, ofile);
2172 else
2173 nasm_write(elf_sects[i].data, len, ofile);
2174 fwritezero(align, ofile);
2178 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
2180 saa_wbytes(sect->data, data, len);
2181 sect->len += len;
2184 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
2186 saa_writeaddr(sect->data, data, len);
2187 sect->len += len;
2190 static void elf_sectalign(int32_t seg, unsigned int value)
2192 struct elf_section *s = NULL;
2193 int i;
2195 for (i = 0; i < nsects; i++) {
2196 if (sects[i]->index == seg) {
2197 s = sects[i];
2198 break;
2201 if (!s || !is_power2(value))
2202 return;
2204 if (value > s->align)
2205 s->align = value;
2208 static int32_t elf_segbase(int32_t segment)
2210 return segment;
2213 static void elf_filename(char *inname, char *outname)
2215 strcpy(elf_module, inname);
2216 standard_extension(inname, outname, ".o");
2219 extern macros_t elf_stdmac[];
2221 static int elf_set_info(enum geninfo type, char **val)
2223 (void)type;
2224 (void)val;
2225 return 0;
2228 static const struct dfmt elf32_df_dwarf = {
2229 "ELF32 (i386) dwarf debug format for Linux/Unix",
2230 "dwarf",
2231 dwarf_init,
2232 dwarf_linenum,
2233 null_debug_deflabel,
2234 null_debug_directive,
2235 debug_typevalue,
2236 dwarf_output,
2237 dwarf_cleanup
2240 static const struct dfmt elf32_df_stabs = {
2241 "ELF32 (i386) stabs debug format for Linux/Unix",
2242 "stabs",
2243 null_debug_init,
2244 stabs_linenum,
2245 null_debug_deflabel,
2246 null_debug_directive,
2247 debug_typevalue,
2248 stabs_output,
2249 stabs_cleanup
2252 static const struct dfmt * const elf32_debugs_arr[3] =
2253 { &elf32_df_dwarf, &elf32_df_stabs, NULL };
2255 const struct ofmt of_elf32 = {
2256 "ELF32 (i386) object files (e.g. Linux)",
2257 "elf32",
2260 elf32_debugs_arr,
2261 &elf32_df_stabs,
2262 elf_stdmac,
2263 elf_init,
2264 elf_set_info,
2265 nasm_do_legacy_output,
2266 elf32_out,
2267 elf_deflabel,
2268 elf_section_names,
2269 elf_sectalign,
2270 elf_segbase,
2271 elf_directive,
2272 elf_filename,
2273 elf_cleanup
2276 static const struct dfmt elf64_df_dwarf = {
2277 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
2278 "dwarf",
2279 dwarf_init,
2280 dwarf_linenum,
2281 null_debug_deflabel,
2282 null_debug_directive,
2283 debug_typevalue,
2284 dwarf_output,
2285 dwarf_cleanup
2288 static const struct dfmt elf64_df_stabs = {
2289 "ELF64 (x86-64) stabs debug format for Linux/Unix",
2290 "stabs",
2291 null_debug_init,
2292 stabs_linenum,
2293 null_debug_deflabel,
2294 null_debug_directive,
2295 debug_typevalue,
2296 stabs_output,
2297 stabs_cleanup
2300 static const struct dfmt * const elf64_debugs_arr[3] =
2301 { &elf64_df_dwarf, &elf64_df_stabs, NULL };
2303 const struct ofmt of_elf64 = {
2304 "ELF64 (x86_64) object files (e.g. Linux)",
2305 "elf64",
2308 elf64_debugs_arr,
2309 &elf64_df_stabs,
2310 elf_stdmac,
2311 elf_init,
2312 elf_set_info,
2313 nasm_do_legacy_output,
2314 elf64_out,
2315 elf_deflabel,
2316 elf_section_names,
2317 elf_sectalign,
2318 elf_segbase,
2319 elf_directive,
2320 elf_filename,
2321 elf_cleanup
2324 static const struct dfmt elfx32_df_dwarf = {
2325 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
2326 "dwarf",
2327 dwarf_init,
2328 dwarf_linenum,
2329 null_debug_deflabel,
2330 null_debug_directive,
2331 debug_typevalue,
2332 dwarf_output,
2333 dwarf_cleanup
2336 static const struct dfmt elfx32_df_stabs = {
2337 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
2338 "stabs",
2339 null_debug_init,
2340 stabs_linenum,
2341 null_debug_deflabel,
2342 null_debug_directive,
2343 debug_typevalue,
2344 stabs_output,
2345 stabs_cleanup
2348 static const struct dfmt * const elfx32_debugs_arr[3] =
2349 { &elfx32_df_dwarf, &elfx32_df_stabs, NULL };
2351 const struct ofmt of_elfx32 = {
2352 "ELFX32 (x86_64) object files (e.g. Linux)",
2353 "elfx32",
2356 elfx32_debugs_arr,
2357 &elfx32_df_stabs,
2358 elf_stdmac,
2359 elf_init,
2360 elf_set_info,
2361 nasm_do_legacy_output,
2362 elfx32_out,
2363 elf_deflabel,
2364 elf_section_names,
2365 elf_sectalign,
2366 elf_segbase,
2367 elf_directive,
2368 elf_filename,
2369 elf_cleanup
2372 static bool is_elf64(void)
2374 return ofmt == &of_elf64;
2377 static bool is_elf32(void)
2379 return ofmt == &of_elf32;
2382 static bool is_elfx32(void)
2384 return ofmt == &of_elfx32;
2387 static bool dfmt_is_stabs(void)
2389 return dfmt == &elf32_df_stabs || dfmt == &elfx32_df_stabs || dfmt == &elf64_df_stabs;
2392 static bool dfmt_is_dwarf(void)
2394 return dfmt == &elf32_df_dwarf || dfmt == &elfx32_df_dwarf || dfmt == &elf64_df_dwarf;
2397 /* common debugging routines */
2398 static void debug_typevalue(int32_t type)
2400 int32_t stype, ssize;
2401 switch (TYM_TYPE(type)) {
2402 case TY_LABEL:
2403 ssize = 0;
2404 stype = STT_NOTYPE;
2405 break;
2406 case TY_BYTE:
2407 ssize = 1;
2408 stype = STT_OBJECT;
2409 break;
2410 case TY_WORD:
2411 ssize = 2;
2412 stype = STT_OBJECT;
2413 break;
2414 case TY_DWORD:
2415 ssize = 4;
2416 stype = STT_OBJECT;
2417 break;
2418 case TY_FLOAT:
2419 ssize = 4;
2420 stype = STT_OBJECT;
2421 break;
2422 case TY_QWORD:
2423 ssize = 8;
2424 stype = STT_OBJECT;
2425 break;
2426 case TY_TBYTE:
2427 ssize = 10;
2428 stype = STT_OBJECT;
2429 break;
2430 case TY_OWORD:
2431 ssize = 16;
2432 stype = STT_OBJECT;
2433 break;
2434 case TY_YWORD:
2435 ssize = 32;
2436 stype = STT_OBJECT;
2437 break;
2438 case TY_COMMON:
2439 ssize = 0;
2440 stype = STT_COMMON;
2441 break;
2442 case TY_SEG:
2443 ssize = 0;
2444 stype = STT_SECTION;
2445 break;
2446 case TY_EXTERN:
2447 ssize = 0;
2448 stype = STT_NOTYPE;
2449 break;
2450 case TY_EQU:
2451 ssize = 0;
2452 stype = STT_NOTYPE;
2453 break;
2454 default:
2455 ssize = 0;
2456 stype = STT_NOTYPE;
2457 break;
2459 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
2460 lastsym->size = ssize;
2461 lastsym->type = stype;
2465 /* stabs debugging routines */
2467 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t segto)
2469 (void)segto;
2470 if (!stabs_filename) {
2471 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
2472 strcpy(stabs_filename, filename);
2473 } else {
2474 if (strcmp(stabs_filename, filename)) {
2475 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2476 in fact, this leak comes in quite handy to maintain a list of files
2477 encountered so far in the symbol lines... */
2479 /* why not nasm_free(stabs_filename); we're done with the old one */
2481 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
2482 strcpy(stabs_filename, filename);
2485 debug_immcall = 1;
2486 currentline = linenumber;
2489 static void stabs_output(int type, void *param)
2491 struct symlininfo *s;
2492 struct linelist *el;
2493 if (type == TY_DEBUGSYMLIN) {
2494 if (debug_immcall) {
2495 s = (struct symlininfo *)param;
2496 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2497 return; /* line info is only collected for executable sections */
2498 numlinestabs++;
2499 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2500 el->info.offset = s->offset;
2501 el->info.section = s->section;
2502 el->info.name = s->name;
2503 el->line = currentline;
2504 el->filename = stabs_filename;
2505 el->next = 0;
2506 if (stabslines) {
2507 stabslines->last->next = el;
2508 stabslines->last = el;
2509 } else {
2510 stabslines = el;
2511 stabslines->last = el;
2515 debug_immcall = 0;
2518 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2520 static void stabs_generate(void)
2522 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
2523 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
2524 char **allfiles;
2525 int *fileidx;
2527 struct linelist *ptr;
2529 ptr = stabslines;
2531 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
2532 numfiles = 0;
2533 while (ptr) {
2534 if (numfiles == 0) {
2535 allfiles[0] = ptr->filename;
2536 numfiles++;
2537 } else {
2538 for (i = 0; i < numfiles; i++) {
2539 if (!strcmp(allfiles[i], ptr->filename))
2540 break;
2542 if (i >= numfiles) {
2543 allfiles[i] = ptr->filename;
2544 numfiles++;
2547 ptr = ptr->next;
2549 strsize = 1;
2550 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
2551 for (i = 0; i < numfiles; i++) {
2552 fileidx[i] = strsize;
2553 strsize += strlen(allfiles[i]) + 1;
2555 mainfileindex = 0;
2556 for (i = 0; i < numfiles; i++) {
2557 if (!strcmp(allfiles[i], elf_module)) {
2558 mainfileindex = i;
2559 break;
2564 * worst case size of the stab buffer would be:
2565 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2566 * plus one "ending" entry
2568 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
2569 sizeof(struct stabentry));
2570 ssbuf = (uint8_t *)nasm_malloc(strsize);
2571 rbuf = (uint8_t *)nasm_malloc(numlinestabs * (is_elf64() ? 16 : 8) * (2 + 3));
2572 rptr = rbuf;
2574 for (i = 0; i < numfiles; i++)
2575 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
2576 ssbuf[0] = 0;
2578 stabstrlen = strsize; /* set global variable for length of stab strings */
2580 sptr = sbuf;
2581 ptr = stabslines;
2582 numstabs = 0;
2584 if (ptr) {
2586 * this is the first stab, its strx points to the filename of the
2587 * the source-file, the n_desc field should be set to the number
2588 * of remaining stabs
2590 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
2592 /* this is the stab for the main source file */
2593 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
2595 /* relocation table entry */
2598 * Since the symbol table has two entries before
2599 * the section symbols, the index in the info.section
2600 * member must be adjusted by adding 2
2603 if (is_elf32()) {
2604 WRITELONG(rptr, (sptr - sbuf) - 4);
2605 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2606 } else if (is_elfx32()) {
2607 WRITELONG(rptr, (sptr - sbuf) - 4);
2608 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2609 WRITELONG(rptr, 0);
2610 } else {
2611 nasm_assert(is_elf64());
2612 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2613 WRITELONG(rptr, R_X86_64_32);
2614 WRITELONG(rptr, ptr->info.section + 2);
2615 WRITEDLONG(rptr, 0);
2617 numstabs++;
2618 currfile = mainfileindex;
2621 if (is_elf32()) {
2622 while (ptr) {
2623 if (strcmp(allfiles[currfile], ptr->filename)) {
2624 /* oops file has changed... */
2625 for (i = 0; i < numfiles; i++)
2626 if (!strcmp(allfiles[i], ptr->filename))
2627 break;
2628 currfile = i;
2629 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2630 ptr->info.offset);
2631 numstabs++;
2633 /* relocation table entry */
2634 WRITELONG(rptr, (sptr - sbuf) - 4);
2635 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2638 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2639 numstabs++;
2641 /* relocation table entry */
2642 WRITELONG(rptr, (sptr - sbuf) - 4);
2643 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2645 ptr = ptr->next;
2647 } else if (is_elfx32()) {
2648 while (ptr) {
2649 if (strcmp(allfiles[currfile], ptr->filename)) {
2650 /* oops file has changed... */
2651 for (i = 0; i < numfiles; i++)
2652 if (!strcmp(allfiles[i], ptr->filename))
2653 break;
2654 currfile = i;
2655 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2656 ptr->info.offset);
2657 numstabs++;
2659 /* relocation table entry */
2660 WRITELONG(rptr, (sptr - sbuf) - 4);
2661 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2662 WRITELONG(rptr, ptr->info.offset);
2665 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2666 numstabs++;
2668 /* relocation table entry */
2669 WRITELONG(rptr, (sptr - sbuf) - 4);
2670 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2671 WRITELONG(rptr, ptr->info.offset);
2673 ptr = ptr->next;
2675 } else {
2676 nasm_assert(is_elf64());
2677 while (ptr) {
2678 if (strcmp(allfiles[currfile], ptr->filename)) {
2679 /* oops file has changed... */
2680 for (i = 0; i < numfiles; i++)
2681 if (!strcmp(allfiles[i], ptr->filename))
2682 break;
2683 currfile = i;
2684 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2685 ptr->info.offset);
2686 numstabs++;
2688 /* relocation table entry */
2689 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2690 WRITELONG(rptr, R_X86_64_32);
2691 WRITELONG(rptr, ptr->info.section + 2);
2692 WRITEDLONG(rptr, ptr->info.offset);
2695 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2696 numstabs++;
2698 /* relocation table entry */
2699 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2700 WRITELONG(rptr, R_X86_64_32);
2701 WRITELONG(rptr, ptr->info.section + 2);
2702 WRITEDLONG(rptr, ptr->info.offset);
2704 ptr = ptr->next;
2708 /* this is an "ending" token */
2709 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
2710 numstabs++;
2712 ((struct stabentry *)sbuf)->n_desc = numstabs;
2714 nasm_free(allfiles);
2715 nasm_free(fileidx);
2717 stablen = (sptr - sbuf);
2718 stabrellen = (rptr - rbuf);
2719 stabrelbuf = rbuf;
2720 stabbuf = sbuf;
2721 stabstrbuf = ssbuf;
2724 static void stabs_cleanup(void)
2726 struct linelist *ptr, *del;
2727 if (!stabslines)
2728 return;
2730 ptr = stabslines;
2731 while (ptr) {
2732 del = ptr;
2733 ptr = ptr->next;
2734 nasm_free(del);
2737 nasm_free(stabbuf);
2738 nasm_free(stabrelbuf);
2739 nasm_free(stabstrbuf);
2742 /* dwarf routines */
2744 static void dwarf_init(void)
2746 ndebugs = 3; /* 3 debug symbols */
2749 static void dwarf_linenum(const char *filename, int32_t linenumber,
2750 int32_t segto)
2752 (void)segto;
2753 dwarf_findfile(filename);
2754 debug_immcall = 1;
2755 currentline = linenumber;
2758 /* called from elf_out with type == TY_DEBUGSYMLIN */
2759 static void dwarf_output(int type, void *param)
2761 int ln, aa, inx, maxln, soc;
2762 struct symlininfo *s;
2763 struct SAA *plinep;
2765 (void)type;
2767 s = (struct symlininfo *)param;
2769 /* line number info is only gathered for executable sections */
2770 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2771 return;
2773 /* Check if section index has changed */
2774 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
2775 dwarf_findsect(s->section);
2777 /* do nothing unless line or file has changed */
2778 if (!debug_immcall)
2779 return;
2781 ln = currentline - dwarf_csect->line;
2782 aa = s->offset - dwarf_csect->offset;
2783 inx = dwarf_clist->line;
2784 plinep = dwarf_csect->psaa;
2785 /* check for file change */
2786 if (!(inx == dwarf_csect->file)) {
2787 saa_write8(plinep,DW_LNS_set_file);
2788 saa_write8(plinep,inx);
2789 dwarf_csect->file = inx;
2791 /* check for line change */
2792 if (ln) {
2793 /* test if in range of special op code */
2794 maxln = line_base + line_range;
2795 soc = (ln - line_base) + (line_range * aa) + opcode_base;
2796 if (ln >= line_base && ln < maxln && soc < 256) {
2797 saa_write8(plinep,soc);
2798 } else {
2799 saa_write8(plinep,DW_LNS_advance_line);
2800 saa_wleb128s(plinep,ln);
2801 if (aa) {
2802 saa_write8(plinep,DW_LNS_advance_pc);
2803 saa_wleb128u(plinep,aa);
2806 dwarf_csect->line = currentline;
2807 dwarf_csect->offset = s->offset;
2810 /* show change handled */
2811 debug_immcall = 0;
2815 static void dwarf_generate(void)
2817 uint8_t *pbuf;
2818 int indx;
2819 struct linelist *ftentry;
2820 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2821 struct SAA *parangesrel, *plinesrel, *pinforel;
2822 struct sectlist *psect;
2823 size_t saalen, linepoff, totlen, highaddr;
2825 if (is_elf32()) {
2826 /* write epilogues for each line program range */
2827 /* and build aranges section */
2828 paranges = saa_init(1L);
2829 parangesrel = saa_init(1L);
2830 saa_write16(paranges,2); /* dwarf version */
2831 saa_write32(parangesrel, paranges->datalen+4);
2832 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
2833 saa_write32(parangesrel, 0);
2834 saa_write32(paranges,0); /* offset into info */
2835 saa_write8(paranges,4); /* pointer size */
2836 saa_write8(paranges,0); /* not segmented */
2837 saa_write32(paranges,0); /* padding */
2838 /* iterate though sectlist entries */
2839 psect = dwarf_fsect;
2840 totlen = 0;
2841 highaddr = 0;
2842 for (indx = 0; indx < dwarf_nsections; indx++) {
2843 plinep = psect->psaa;
2844 /* Line Number Program Epilogue */
2845 saa_write8(plinep,2); /* std op 2 */
2846 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2847 saa_write8(plinep,DW_LNS_extended_op);
2848 saa_write8(plinep,1); /* operand length */
2849 saa_write8(plinep,DW_LNE_end_sequence);
2850 totlen += plinep->datalen;
2851 /* range table relocation entry */
2852 saa_write32(parangesrel, paranges->datalen + 4);
2853 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2854 saa_write32(parangesrel, (uint32_t) 0);
2855 /* range table entry */
2856 saa_write32(paranges,0x0000); /* range start */
2857 saa_write32(paranges,sects[psect->section]->len); /* range length */
2858 highaddr += sects[psect->section]->len;
2859 /* done with this entry */
2860 psect = psect->next;
2862 saa_write32(paranges,0); /* null address */
2863 saa_write32(paranges,0); /* null length */
2864 saalen = paranges->datalen;
2865 arangeslen = saalen + 4;
2866 arangesbuf = pbuf = nasm_malloc(arangeslen);
2867 WRITELONG(pbuf,saalen); /* initial length */
2868 saa_rnbytes(paranges, pbuf, saalen);
2869 saa_free(paranges);
2870 } else if (is_elfx32()) {
2871 /* write epilogues for each line program range */
2872 /* and build aranges section */
2873 paranges = saa_init(1L);
2874 parangesrel = saa_init(1L);
2875 saa_write16(paranges,3); /* dwarf version */
2876 saa_write32(parangesrel, paranges->datalen+4);
2877 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
2878 saa_write32(parangesrel, 0);
2879 saa_write32(paranges,0); /* offset into info */
2880 saa_write8(paranges,4); /* pointer size */
2881 saa_write8(paranges,0); /* not segmented */
2882 saa_write32(paranges,0); /* padding */
2883 /* iterate though sectlist entries */
2884 psect = dwarf_fsect;
2885 totlen = 0;
2886 highaddr = 0;
2887 for (indx = 0; indx < dwarf_nsections; indx++) {
2888 plinep = psect->psaa;
2889 /* Line Number Program Epilogue */
2890 saa_write8(plinep,2); /* std op 2 */
2891 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2892 saa_write8(plinep,DW_LNS_extended_op);
2893 saa_write8(plinep,1); /* operand length */
2894 saa_write8(plinep,DW_LNE_end_sequence);
2895 totlen += plinep->datalen;
2896 /* range table relocation entry */
2897 saa_write32(parangesrel, paranges->datalen + 4);
2898 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
2899 saa_write32(parangesrel, (uint32_t) 0);
2900 /* range table entry */
2901 saa_write32(paranges,0x0000); /* range start */
2902 saa_write32(paranges,sects[psect->section]->len); /* range length */
2903 highaddr += sects[psect->section]->len;
2904 /* done with this entry */
2905 psect = psect->next;
2907 saa_write32(paranges,0); /* null address */
2908 saa_write32(paranges,0); /* null length */
2909 saalen = paranges->datalen;
2910 arangeslen = saalen + 4;
2911 arangesbuf = pbuf = nasm_malloc(arangeslen);
2912 WRITELONG(pbuf,saalen); /* initial length */
2913 saa_rnbytes(paranges, pbuf, saalen);
2914 saa_free(paranges);
2915 } else {
2916 nasm_assert(is_elf64());
2917 /* write epilogues for each line program range */
2918 /* and build aranges section */
2919 paranges = saa_init(1L);
2920 parangesrel = saa_init(1L);
2921 saa_write16(paranges,3); /* dwarf version */
2922 saa_write64(parangesrel, paranges->datalen+4);
2923 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2924 saa_write64(parangesrel, 0);
2925 saa_write32(paranges,0); /* offset into info */
2926 saa_write8(paranges,8); /* pointer size */
2927 saa_write8(paranges,0); /* not segmented */
2928 saa_write32(paranges,0); /* padding */
2929 /* iterate though sectlist entries */
2930 psect = dwarf_fsect;
2931 totlen = 0;
2932 highaddr = 0;
2933 for (indx = 0; indx < dwarf_nsections; indx++) {
2934 plinep = psect->psaa;
2935 /* Line Number Program Epilogue */
2936 saa_write8(plinep,2); /* std op 2 */
2937 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2938 saa_write8(plinep,DW_LNS_extended_op);
2939 saa_write8(plinep,1); /* operand length */
2940 saa_write8(plinep,DW_LNE_end_sequence);
2941 totlen += plinep->datalen;
2942 /* range table relocation entry */
2943 saa_write64(parangesrel, paranges->datalen + 4);
2944 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2945 saa_write64(parangesrel, (uint64_t) 0);
2946 /* range table entry */
2947 saa_write64(paranges,0x0000); /* range start */
2948 saa_write64(paranges,sects[psect->section]->len); /* range length */
2949 highaddr += sects[psect->section]->len;
2950 /* done with this entry */
2951 psect = psect->next;
2953 saa_write64(paranges,0); /* null address */
2954 saa_write64(paranges,0); /* null length */
2955 saalen = paranges->datalen;
2956 arangeslen = saalen + 4;
2957 arangesbuf = pbuf = nasm_malloc(arangeslen);
2958 WRITELONG(pbuf,saalen); /* initial length */
2959 saa_rnbytes(paranges, pbuf, saalen);
2960 saa_free(paranges);
2963 /* build rela.aranges section */
2964 arangesrellen = saalen = parangesrel->datalen;
2965 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2966 saa_rnbytes(parangesrel, pbuf, saalen);
2967 saa_free(parangesrel);
2969 /* build pubnames section */
2970 ppubnames = saa_init(1L);
2971 saa_write16(ppubnames,3); /* dwarf version */
2972 saa_write32(ppubnames,0); /* offset into info */
2973 saa_write32(ppubnames,0); /* space used in info */
2974 saa_write32(ppubnames,0); /* end of list */
2975 saalen = ppubnames->datalen;
2976 pubnameslen = saalen + 4;
2977 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2978 WRITELONG(pbuf,saalen); /* initial length */
2979 saa_rnbytes(ppubnames, pbuf, saalen);
2980 saa_free(ppubnames);
2982 if (is_elf32()) {
2983 /* build info section */
2984 pinfo = saa_init(1L);
2985 pinforel = saa_init(1L);
2986 saa_write16(pinfo,2); /* dwarf version */
2987 saa_write32(pinforel, pinfo->datalen + 4);
2988 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
2989 saa_write32(pinforel, 0);
2990 saa_write32(pinfo,0); /* offset into abbrev */
2991 saa_write8(pinfo,4); /* pointer size */
2992 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2993 saa_write32(pinforel, pinfo->datalen + 4);
2994 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2995 saa_write32(pinforel, 0);
2996 saa_write32(pinfo,0); /* DW_AT_low_pc */
2997 saa_write32(pinforel, pinfo->datalen + 4);
2998 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2999 saa_write32(pinforel, 0);
3000 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
3001 saa_write32(pinforel, pinfo->datalen + 4);
3002 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
3003 saa_write32(pinforel, 0);
3004 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3005 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3006 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3007 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3008 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3009 saa_write32(pinforel, pinfo->datalen + 4);
3010 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3011 saa_write32(pinforel, 0);
3012 saa_write32(pinfo,0); /* DW_AT_low_pc */
3013 saa_write32(pinfo,0); /* DW_AT_frame_base */
3014 saa_write8(pinfo,0); /* end of entries */
3015 saalen = pinfo->datalen;
3016 infolen = saalen + 4;
3017 infobuf = pbuf = nasm_malloc(infolen);
3018 WRITELONG(pbuf,saalen); /* initial length */
3019 saa_rnbytes(pinfo, pbuf, saalen);
3020 saa_free(pinfo);
3021 } else if (is_elfx32()) {
3022 /* build info section */
3023 pinfo = saa_init(1L);
3024 pinforel = saa_init(1L);
3025 saa_write16(pinfo,3); /* dwarf version */
3026 saa_write32(pinforel, pinfo->datalen + 4);
3027 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
3028 saa_write32(pinforel, 0);
3029 saa_write32(pinfo,0); /* offset into abbrev */
3030 saa_write8(pinfo,4); /* pointer size */
3031 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3032 saa_write32(pinforel, pinfo->datalen + 4);
3033 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3034 saa_write32(pinforel, 0);
3035 saa_write32(pinfo,0); /* DW_AT_low_pc */
3036 saa_write32(pinforel, pinfo->datalen + 4);
3037 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3038 saa_write32(pinforel, 0);
3039 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
3040 saa_write32(pinforel, pinfo->datalen + 4);
3041 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
3042 saa_write32(pinforel, 0);
3043 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3044 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3045 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3046 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3047 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3048 saa_write32(pinforel, pinfo->datalen + 4);
3049 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3050 saa_write32(pinforel, 0);
3051 saa_write32(pinfo,0); /* DW_AT_low_pc */
3052 saa_write32(pinfo,0); /* DW_AT_frame_base */
3053 saa_write8(pinfo,0); /* end of entries */
3054 saalen = pinfo->datalen;
3055 infolen = saalen + 4;
3056 infobuf = pbuf = nasm_malloc(infolen);
3057 WRITELONG(pbuf,saalen); /* initial length */
3058 saa_rnbytes(pinfo, pbuf, saalen);
3059 saa_free(pinfo);
3060 } else {
3061 nasm_assert(is_elf64());
3062 /* build info section */
3063 pinfo = saa_init(1L);
3064 pinforel = saa_init(1L);
3065 saa_write16(pinfo,3); /* dwarf version */
3066 saa_write64(pinforel, pinfo->datalen + 4);
3067 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
3068 saa_write64(pinforel, 0);
3069 saa_write32(pinfo,0); /* offset into abbrev */
3070 saa_write8(pinfo,8); /* pointer size */
3071 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3072 saa_write64(pinforel, pinfo->datalen + 4);
3073 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3074 saa_write64(pinforel, 0);
3075 saa_write64(pinfo,0); /* DW_AT_low_pc */
3076 saa_write64(pinforel, pinfo->datalen + 4);
3077 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3078 saa_write64(pinforel, 0);
3079 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
3080 saa_write64(pinforel, pinfo->datalen + 4);
3081 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
3082 saa_write64(pinforel, 0);
3083 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3084 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3085 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3086 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3087 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3088 saa_write64(pinforel, pinfo->datalen + 4);
3089 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3090 saa_write64(pinforel, 0);
3091 saa_write64(pinfo,0); /* DW_AT_low_pc */
3092 saa_write64(pinfo,0); /* DW_AT_frame_base */
3093 saa_write8(pinfo,0); /* end of entries */
3094 saalen = pinfo->datalen;
3095 infolen = saalen + 4;
3096 infobuf = pbuf = nasm_malloc(infolen);
3097 WRITELONG(pbuf,saalen); /* initial length */
3098 saa_rnbytes(pinfo, pbuf, saalen);
3099 saa_free(pinfo);
3102 /* build rela.info section */
3103 inforellen = saalen = pinforel->datalen;
3104 inforelbuf = pbuf = nasm_malloc(inforellen);
3105 saa_rnbytes(pinforel, pbuf, saalen);
3106 saa_free(pinforel);
3108 /* build abbrev section */
3109 pabbrev = saa_init(1L);
3110 saa_write8(pabbrev,1); /* entry number LEB128u */
3111 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
3112 saa_write8(pabbrev,1); /* has children */
3113 /* the following attributes and forms are all LEB128u values */
3114 saa_write8(pabbrev,DW_AT_low_pc);
3115 saa_write8(pabbrev,DW_FORM_addr);
3116 saa_write8(pabbrev,DW_AT_high_pc);
3117 saa_write8(pabbrev,DW_FORM_addr);
3118 saa_write8(pabbrev,DW_AT_stmt_list);
3119 saa_write8(pabbrev,DW_FORM_data4);
3120 saa_write8(pabbrev,DW_AT_name);
3121 saa_write8(pabbrev,DW_FORM_string);
3122 saa_write8(pabbrev,DW_AT_producer);
3123 saa_write8(pabbrev,DW_FORM_string);
3124 saa_write8(pabbrev,DW_AT_language);
3125 saa_write8(pabbrev,DW_FORM_data2);
3126 saa_write16(pabbrev,0); /* end of entry */
3127 /* LEB128u usage same as above */
3128 saa_write8(pabbrev,2); /* entry number */
3129 saa_write8(pabbrev,DW_TAG_subprogram);
3130 saa_write8(pabbrev,0); /* no children */
3131 saa_write8(pabbrev,DW_AT_low_pc);
3132 saa_write8(pabbrev,DW_FORM_addr);
3133 saa_write8(pabbrev,DW_AT_frame_base);
3134 saa_write8(pabbrev,DW_FORM_data4);
3135 saa_write16(pabbrev,0); /* end of entry */
3136 abbrevlen = saalen = pabbrev->datalen;
3137 abbrevbuf = pbuf = nasm_malloc(saalen);
3138 saa_rnbytes(pabbrev, pbuf, saalen);
3139 saa_free(pabbrev);
3141 /* build line section */
3142 /* prolog */
3143 plines = saa_init(1L);
3144 saa_write8(plines,1); /* Minimum Instruction Length */
3145 saa_write8(plines,1); /* Initial value of 'is_stmt' */
3146 saa_write8(plines,line_base); /* Line Base */
3147 saa_write8(plines,line_range); /* Line Range */
3148 saa_write8(plines,opcode_base); /* Opcode Base */
3149 /* standard opcode lengths (# of LEB128u operands) */
3150 saa_write8(plines,0); /* Std opcode 1 length */
3151 saa_write8(plines,1); /* Std opcode 2 length */
3152 saa_write8(plines,1); /* Std opcode 3 length */
3153 saa_write8(plines,1); /* Std opcode 4 length */
3154 saa_write8(plines,1); /* Std opcode 5 length */
3155 saa_write8(plines,0); /* Std opcode 6 length */
3156 saa_write8(plines,0); /* Std opcode 7 length */
3157 saa_write8(plines,0); /* Std opcode 8 length */
3158 saa_write8(plines,1); /* Std opcode 9 length */
3159 saa_write8(plines,0); /* Std opcode 10 length */
3160 saa_write8(plines,0); /* Std opcode 11 length */
3161 saa_write8(plines,1); /* Std opcode 12 length */
3162 /* Directory Table */
3163 saa_write8(plines,0); /* End of table */
3164 /* File Name Table */
3165 ftentry = dwarf_flist;
3166 for (indx = 0; indx < dwarf_numfiles; indx++) {
3167 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
3168 saa_write8(plines,0); /* directory LEB128u */
3169 saa_write8(plines,0); /* time LEB128u */
3170 saa_write8(plines,0); /* size LEB128u */
3171 ftentry = ftentry->next;
3173 saa_write8(plines,0); /* End of table */
3174 linepoff = plines->datalen;
3175 linelen = linepoff + totlen + 10;
3176 linebuf = pbuf = nasm_malloc(linelen);
3177 WRITELONG(pbuf,linelen-4); /* initial length */
3178 WRITESHORT(pbuf,3); /* dwarf version */
3179 WRITELONG(pbuf,linepoff); /* offset to line number program */
3180 /* write line header */
3181 saalen = linepoff;
3182 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
3183 pbuf += linepoff;
3184 saa_free(plines);
3185 /* concatonate line program ranges */
3186 linepoff += 13;
3187 plinesrel = saa_init(1L);
3188 psect = dwarf_fsect;
3189 if (is_elf32()) {
3190 for (indx = 0; indx < dwarf_nsections; indx++) {
3191 saa_write32(plinesrel, linepoff);
3192 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
3193 saa_write32(plinesrel, (uint32_t) 0);
3194 plinep = psect->psaa;
3195 saalen = plinep->datalen;
3196 saa_rnbytes(plinep, pbuf, saalen);
3197 pbuf += saalen;
3198 linepoff += saalen;
3199 saa_free(plinep);
3200 /* done with this entry */
3201 psect = psect->next;
3203 } else if (is_elfx32()) {
3204 for (indx = 0; indx < dwarf_nsections; indx++) {
3205 saa_write32(plinesrel, linepoff);
3206 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
3207 saa_write32(plinesrel, 0);
3208 plinep = psect->psaa;
3209 saalen = plinep->datalen;
3210 saa_rnbytes(plinep, pbuf, saalen);
3211 pbuf += saalen;
3212 linepoff += saalen;
3213 saa_free(plinep);
3214 /* done with this entry */
3215 psect = psect->next;
3217 } else {
3218 nasm_assert(is_elf64());
3219 for (indx = 0; indx < dwarf_nsections; indx++) {
3220 saa_write64(plinesrel, linepoff);
3221 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
3222 saa_write64(plinesrel, (uint64_t) 0);
3223 plinep = psect->psaa;
3224 saalen = plinep->datalen;
3225 saa_rnbytes(plinep, pbuf, saalen);
3226 pbuf += saalen;
3227 linepoff += saalen;
3228 saa_free(plinep);
3229 /* done with this entry */
3230 psect = psect->next;
3234 /* build rela.lines section */
3235 linerellen =saalen = plinesrel->datalen;
3236 linerelbuf = pbuf = nasm_malloc(linerellen);
3237 saa_rnbytes(plinesrel, pbuf, saalen);
3238 saa_free(plinesrel);
3240 /* build frame section */
3241 framelen = 4;
3242 framebuf = pbuf = nasm_malloc(framelen);
3243 WRITELONG(pbuf,framelen-4); /* initial length */
3245 /* build loc section */
3246 loclen = 16;
3247 locbuf = pbuf = nasm_malloc(loclen);
3248 if (is_elf32()) {
3249 WRITELONG(pbuf,0); /* null beginning offset */
3250 WRITELONG(pbuf,0); /* null ending offset */
3251 } else {
3252 nasm_assert(is_elf64());
3253 WRITEDLONG(pbuf,0); /* null beginning offset */
3254 WRITEDLONG(pbuf,0); /* null ending offset */
3258 static void dwarf_cleanup(void)
3260 nasm_free(arangesbuf);
3261 nasm_free(arangesrelbuf);
3262 nasm_free(pubnamesbuf);
3263 nasm_free(infobuf);
3264 nasm_free(inforelbuf);
3265 nasm_free(abbrevbuf);
3266 nasm_free(linebuf);
3267 nasm_free(linerelbuf);
3268 nasm_free(framebuf);
3269 nasm_free(locbuf);
3272 static void dwarf_findfile(const char * fname)
3274 int finx;
3275 struct linelist *match;
3277 /* return if fname is current file name */
3278 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
3279 return;
3281 /* search for match */
3282 match = 0;
3283 if (dwarf_flist) {
3284 match = dwarf_flist;
3285 for (finx = 0; finx < dwarf_numfiles; finx++) {
3286 if (!(strcmp(fname, match->filename))) {
3287 dwarf_clist = match;
3288 return;
3293 /* add file name to end of list */
3294 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
3295 dwarf_numfiles++;
3296 dwarf_clist->line = dwarf_numfiles;
3297 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
3298 strcpy(dwarf_clist->filename,fname);
3299 dwarf_clist->next = 0;
3300 if (!dwarf_flist) { /* if first entry */
3301 dwarf_flist = dwarf_elist = dwarf_clist;
3302 dwarf_clist->last = 0;
3303 } else { /* chain to previous entry */
3304 dwarf_elist->next = dwarf_clist;
3305 dwarf_elist = dwarf_clist;
3309 static void dwarf_findsect(const int index)
3311 int sinx;
3312 struct sectlist *match;
3313 struct SAA *plinep;
3315 /* return if index is current section index */
3316 if (dwarf_csect && (dwarf_csect->section == index))
3317 return;
3319 /* search for match */
3320 match = 0;
3321 if (dwarf_fsect) {
3322 match = dwarf_fsect;
3323 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
3324 if (match->section == index) {
3325 dwarf_csect = match;
3326 return;
3328 match = match->next;
3332 /* add entry to end of list */
3333 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
3334 dwarf_nsections++;
3335 dwarf_csect->psaa = plinep = saa_init(1L);
3336 dwarf_csect->line = 1;
3337 dwarf_csect->offset = 0;
3338 dwarf_csect->file = 1;
3339 dwarf_csect->section = index;
3340 dwarf_csect->next = 0;
3341 /* set relocatable address at start of line program */
3342 saa_write8(plinep,DW_LNS_extended_op);
3343 saa_write8(plinep,is_elf64() ? 9 : 5); /* operand length */
3344 saa_write8(plinep,DW_LNE_set_address);
3345 if (is_elf64())
3346 saa_write64(plinep,0); /* Start Address */
3347 else
3348 saa_write32(plinep,0); /* Start Address */
3350 if (!dwarf_fsect) { /* if first entry */
3351 dwarf_fsect = dwarf_esect = dwarf_csect;
3352 dwarf_csect->last = 0;
3353 } else { /* chain to previous entry */
3354 dwarf_esect->next = dwarf_csect;
3355 dwarf_esect = dwarf_csect;
3359 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */