output: elf -- Generate SHT_RELA relocs for elfx32/elf64 stabs
[nasm.git] / output / outelf.c
blobb9075a8f1f5ddb4e8bd00bad731e36d585b89ea4
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;
1005 static void elf64_out(int32_t segto, const void *data,
1006 enum out_type type, uint64_t size,
1007 int32_t segment, int32_t wrt)
1009 struct elf_section *s;
1010 int64_t addr;
1011 int reltype, bytes;
1012 int i;
1013 static struct symlininfo sinfo;
1016 * handle absolute-assembly (structure definitions)
1018 if (segto == NO_SEG) {
1019 if (type != OUT_RESERVE)
1020 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1021 " space");
1022 return;
1025 s = NULL;
1026 for (i = 0; i < nsects; i++)
1027 if (segto == sects[i]->index) {
1028 s = sects[i];
1029 break;
1031 if (!s) {
1032 int tempint; /* ignored */
1033 if (segto != elf_section_names(".text", 2, &tempint))
1034 nasm_panic(0, "strange segment conditions in ELF driver");
1035 else {
1036 s = sects[nsects - 1];
1037 i = nsects - 1;
1041 /* again some stabs debugging stuff */
1042 sinfo.offset = s->len;
1043 sinfo.section = i;
1044 sinfo.segto = segto;
1045 sinfo.name = s->name;
1046 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1047 /* end of debugging stuff */
1049 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1050 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1051 " BSS section `%s': ignored", s->name);
1052 s->len += realsize(type, size);
1053 return;
1056 switch (type) {
1057 case OUT_RESERVE:
1058 if (s->type == SHT_PROGBITS) {
1059 nasm_error(ERR_WARNING, "uninitialized space declared in"
1060 " non-BSS section `%s': zeroing", s->name);
1061 elf_sect_write(s, NULL, size);
1062 } else
1063 s->len += size;
1064 break;
1066 case OUT_RAWDATA:
1067 if (segment != NO_SEG)
1068 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1069 elf_sect_write(s, data, size);
1070 break;
1072 case OUT_ADDRESS:
1074 int isize = (int)size;
1075 int asize = abs((int)size);
1077 addr = *(int64_t *)data;
1078 if (segment == NO_SEG) {
1079 /* Do nothing */
1080 } else if (segment % 2) {
1081 nasm_error(ERR_NONFATAL, "ELF format does not support"
1082 " segment base references");
1083 } else {
1084 if (wrt == NO_SEG) {
1085 switch (isize) {
1086 case 1:
1087 case -1:
1088 elf_add_reloc(s, segment, addr, R_X86_64_8);
1089 break;
1090 case 2:
1091 case -2:
1092 elf_add_reloc(s, segment, addr, R_X86_64_16);
1093 break;
1094 case 4:
1095 elf_add_reloc(s, segment, addr, R_X86_64_32);
1096 break;
1097 case -4:
1098 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1099 break;
1100 case 8:
1101 case -8:
1102 elf_add_reloc(s, segment, addr, R_X86_64_64);
1103 break;
1104 default:
1105 nasm_panic(0, "internal error elf64-hpa-871");
1106 break;
1108 addr = 0;
1109 } else if (wrt == elf_gotpc_sect + 1) {
1111 * The user will supply GOT relative to $$. ELF
1112 * will let us have GOT relative to $. So we
1113 * need to fix up the data item by $-$$.
1115 addr += s->len;
1116 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1117 addr = 0;
1118 } else if (wrt == elf_gotoff_sect + 1) {
1119 if (asize != 8) {
1120 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
1121 "references to be qword");
1122 } else {
1123 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
1124 addr = 0;
1126 } else if (wrt == elf_got_sect + 1) {
1127 switch (asize) {
1128 case 4:
1129 elf_add_gsym_reloc(s, segment, addr, 0,
1130 R_X86_64_GOT32, true);
1131 addr = 0;
1132 break;
1133 case 8:
1134 elf_add_gsym_reloc(s, segment, addr, 0,
1135 R_X86_64_GOT64, true);
1136 addr = 0;
1137 break;
1138 default:
1139 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1140 break;
1142 } else if (wrt == elf_sym_sect + 1) {
1143 switch (isize) {
1144 case 1:
1145 case -1:
1146 elf_add_gsym_reloc(s, segment, addr, 0,
1147 R_X86_64_8, false);
1148 addr = 0;
1149 break;
1150 case 2:
1151 case -2:
1152 elf_add_gsym_reloc(s, segment, addr, 0,
1153 R_X86_64_16, false);
1154 addr = 0;
1155 break;
1156 case 4:
1157 elf_add_gsym_reloc(s, segment, addr, 0,
1158 R_X86_64_32, false);
1159 addr = 0;
1160 break;
1161 case -4:
1162 elf_add_gsym_reloc(s, segment, addr, 0,
1163 R_X86_64_32S, false);
1164 addr = 0;
1165 break;
1166 case 8:
1167 case -8:
1168 elf_add_gsym_reloc(s, segment, addr, 0,
1169 R_X86_64_64, false);
1170 addr = 0;
1171 break;
1172 default:
1173 nasm_panic(0, "internal error elf64-hpa-903");
1174 break;
1176 } else if (wrt == elf_plt_sect + 1) {
1177 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1178 "relative PLT references");
1179 } else {
1180 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1181 " use of WRT");
1184 elf_sect_writeaddr(s, addr, asize);
1185 break;
1188 case OUT_REL1ADR:
1189 reltype = R_X86_64_PC8;
1190 bytes = 1;
1191 goto rel12adr;
1193 case OUT_REL2ADR:
1194 reltype = R_X86_64_PC16;
1195 bytes = 2;
1196 goto rel12adr;
1198 rel12adr:
1199 addr = *(int64_t *)data - size;
1200 if (segment == segto)
1201 nasm_panic(0, "intra-segment OUT_REL1ADR");
1202 if (segment == NO_SEG) {
1203 /* Do nothing */
1204 } else if (segment % 2) {
1205 nasm_error(ERR_NONFATAL, "ELF format does not support"
1206 " segment base references");
1207 } else {
1208 if (wrt == NO_SEG) {
1209 elf_add_reloc(s, segment, addr, reltype);
1210 addr = 0;
1211 } else {
1212 nasm_error(ERR_NONFATAL,
1213 "Unsupported non-32-bit ELF relocation");
1216 elf_sect_writeaddr(s, addr, bytes);
1217 break;
1219 case OUT_REL4ADR:
1220 addr = *(int64_t *)data - size;
1221 if (segment == segto)
1222 nasm_panic(0, "intra-segment OUT_REL4ADR");
1223 if (segment == NO_SEG) {
1224 /* Do nothing */
1225 } else if (segment % 2) {
1226 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1227 " segment base references");
1228 } else {
1229 if (wrt == NO_SEG) {
1230 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1231 addr = 0;
1232 } else if (wrt == elf_plt_sect + 1) {
1233 elf_add_gsym_reloc(s, segment, addr+size, size,
1234 R_X86_64_PLT32, true);
1235 addr = 0;
1236 } else if (wrt == elf_gotpc_sect + 1 ||
1237 wrt == elf_got_sect + 1) {
1238 elf_add_gsym_reloc(s, segment, addr+size, size,
1239 R_X86_64_GOTPCREL, true);
1240 addr = 0;
1241 } else if (wrt == elf_gotoff_sect + 1 ||
1242 wrt == elf_got_sect + 1) {
1243 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1244 "qword absolute");
1245 } else if (wrt == elf_gottpoff_sect + 1) {
1246 elf_add_gsym_reloc(s, segment, addr+size, size,
1247 R_X86_64_GOTTPOFF, true);
1248 addr = 0;
1249 } else {
1250 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1251 " use of WRT");
1254 elf_sect_writeaddr(s, addr, 4);
1255 break;
1257 case OUT_REL8ADR:
1258 addr = *(int64_t *)data - size;
1259 if (segment == segto)
1260 nasm_panic(0, "intra-segment OUT_REL8ADR");
1261 if (segment == NO_SEG) {
1262 /* Do nothing */
1263 } else if (segment % 2) {
1264 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1265 " segment base references");
1266 } else {
1267 if (wrt == NO_SEG) {
1268 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1269 addr = 0;
1270 } else if (wrt == elf_gotpc_sect + 1 ||
1271 wrt == elf_got_sect + 1) {
1272 elf_add_gsym_reloc(s, segment, addr+size, size,
1273 R_X86_64_GOTPCREL64, true);
1274 addr = 0;
1275 } else if (wrt == elf_gotoff_sect + 1 ||
1276 wrt == elf_got_sect + 1) {
1277 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1278 "absolute");
1279 } else if (wrt == elf_gottpoff_sect + 1) {
1280 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1281 "dword");
1282 } else {
1283 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1284 " use of WRT");
1287 elf_sect_writeaddr(s, addr, 8);
1288 break;
1292 static void elfx32_out(int32_t segto, const void *data,
1293 enum out_type type, uint64_t size,
1294 int32_t segment, int32_t wrt)
1296 struct elf_section *s;
1297 int64_t addr;
1298 int reltype, bytes;
1299 int i;
1300 static struct symlininfo sinfo;
1303 * handle absolute-assembly (structure definitions)
1305 if (segto == NO_SEG) {
1306 if (type != OUT_RESERVE)
1307 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1308 " space");
1309 return;
1312 s = NULL;
1313 for (i = 0; i < nsects; i++)
1314 if (segto == sects[i]->index) {
1315 s = sects[i];
1316 break;
1318 if (!s) {
1319 int tempint; /* ignored */
1320 if (segto != elf_section_names(".text", 2, &tempint))
1321 nasm_panic(0, "strange segment conditions in ELF driver");
1322 else {
1323 s = sects[nsects - 1];
1324 i = nsects - 1;
1328 /* again some stabs debugging stuff */
1329 sinfo.offset = s->len;
1330 sinfo.section = i;
1331 sinfo.segto = segto;
1332 sinfo.name = s->name;
1333 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1334 /* end of debugging stuff */
1336 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1337 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1338 " BSS section `%s': ignored", s->name);
1339 s->len += realsize(type, size);
1340 return;
1343 switch (type) {
1344 case OUT_RESERVE:
1345 if (s->type == SHT_PROGBITS) {
1346 nasm_error(ERR_WARNING, "uninitialized space declared in"
1347 " non-BSS section `%s': zeroing", s->name);
1348 elf_sect_write(s, NULL, size);
1349 } else
1350 s->len += size;
1351 break;
1353 case OUT_RAWDATA:
1354 if (segment != NO_SEG)
1355 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1356 elf_sect_write(s, data, size);
1357 break;
1359 case OUT_ADDRESS:
1361 int isize = (int)size;
1362 int asize = abs((int)size);
1364 addr = *(int64_t *)data;
1365 if (segment == NO_SEG) {
1366 /* Do nothing */
1367 } else if (segment % 2) {
1368 nasm_error(ERR_NONFATAL, "ELF format does not support"
1369 " segment base references");
1370 } else {
1371 if (wrt == NO_SEG) {
1372 switch (isize) {
1373 case 1:
1374 case -1:
1375 elf_add_reloc(s, segment, addr, R_X86_64_8);
1376 break;
1377 case 2:
1378 case -2:
1379 elf_add_reloc(s, segment, addr, R_X86_64_16);
1380 break;
1381 case 4:
1382 elf_add_reloc(s, segment, addr, R_X86_64_32);
1383 break;
1384 case -4:
1385 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1386 break;
1387 case 8:
1388 case -8:
1389 elf_add_reloc(s, segment, addr, R_X86_64_64);
1390 break;
1391 default:
1392 nasm_panic(0, "internal error elfx32-hpa-871");
1393 break;
1395 addr = 0;
1396 } else if (wrt == elf_gotpc_sect + 1) {
1398 * The user will supply GOT relative to $$. ELF
1399 * will let us have GOT relative to $. So we
1400 * need to fix up the data item by $-$$.
1402 addr += s->len;
1403 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1404 addr = 0;
1405 } else if (wrt == elf_gotoff_sect + 1) {
1406 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
1407 "R_X86_64_GOTOFF64");
1408 } else if (wrt == elf_got_sect + 1) {
1409 switch (asize) {
1410 case 4:
1411 elf_add_gsym_reloc(s, segment, addr, 0,
1412 R_X86_64_GOT32, true);
1413 addr = 0;
1414 break;
1415 default:
1416 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1417 break;
1419 } else if (wrt == elf_sym_sect + 1) {
1420 switch (isize) {
1421 case 1:
1422 case -1:
1423 elf_add_gsym_reloc(s, segment, addr, 0,
1424 R_X86_64_8, false);
1425 addr = 0;
1426 break;
1427 case 2:
1428 case -2:
1429 elf_add_gsym_reloc(s, segment, addr, 0,
1430 R_X86_64_16, false);
1431 addr = 0;
1432 break;
1433 case 4:
1434 elf_add_gsym_reloc(s, segment, addr, 0,
1435 R_X86_64_32, false);
1436 addr = 0;
1437 break;
1438 case -4:
1439 elf_add_gsym_reloc(s, segment, addr, 0,
1440 R_X86_64_32S, false);
1441 addr = 0;
1442 break;
1443 case 8:
1444 case -8:
1445 elf_add_gsym_reloc(s, segment, addr, 0,
1446 R_X86_64_64, false);
1447 addr = 0;
1448 break;
1449 default:
1450 nasm_panic(0, "internal error elfx32-hpa-903");
1451 break;
1453 } else if (wrt == elf_plt_sect + 1) {
1454 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1455 "relative PLT references");
1456 } else {
1457 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1458 " use of WRT");
1461 elf_sect_writeaddr(s, addr, asize);
1462 break;
1465 case OUT_REL1ADR:
1466 reltype = R_X86_64_PC8;
1467 bytes = 1;
1468 goto rel12adr;
1470 case OUT_REL2ADR:
1471 reltype = R_X86_64_PC16;
1472 bytes = 2;
1473 goto rel12adr;
1475 rel12adr:
1476 addr = *(int64_t *)data - size;
1477 if (segment == segto)
1478 nasm_panic(0, "intra-segment OUT_REL1ADR");
1479 if (segment == NO_SEG) {
1480 /* Do nothing */
1481 } else if (segment % 2) {
1482 nasm_error(ERR_NONFATAL, "ELF format does not support"
1483 " segment base references");
1484 } else {
1485 if (wrt == NO_SEG) {
1486 elf_add_reloc(s, segment, addr, reltype);
1487 addr = 0;
1488 } else {
1489 nasm_error(ERR_NONFATAL,
1490 "Unsupported non-32-bit ELF relocation");
1493 elf_sect_writeaddr(s, addr, bytes);
1494 break;
1496 case OUT_REL4ADR:
1497 addr = *(int64_t *)data - size;
1498 if (segment == segto)
1499 nasm_panic(0, "intra-segment OUT_REL4ADR");
1500 if (segment == NO_SEG) {
1501 /* Do nothing */
1502 } else if (segment % 2) {
1503 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
1504 " segment base references");
1505 } else {
1506 if (wrt == NO_SEG) {
1507 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1508 addr = 0;
1509 } else if (wrt == elf_plt_sect + 1) {
1510 elf_add_gsym_reloc(s, segment, addr+size, size,
1511 R_X86_64_PLT32, true);
1512 addr = 0;
1513 } else if (wrt == elf_gotpc_sect + 1 ||
1514 wrt == elf_got_sect + 1) {
1515 elf_add_gsym_reloc(s, segment, addr+size, size,
1516 R_X86_64_GOTPCREL, true);
1517 addr = 0;
1518 } else if (wrt == elf_gotoff_sect + 1 ||
1519 wrt == elf_got_sect + 1) {
1520 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
1521 } else if (wrt == elf_gottpoff_sect + 1) {
1522 elf_add_gsym_reloc(s, segment, addr+size, size,
1523 R_X86_64_GOTTPOFF, true);
1524 addr = 0;
1525 } else {
1526 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
1527 " use of WRT");
1530 elf_sect_writeaddr(s, addr, 4);
1531 break;
1533 case OUT_REL8ADR:
1534 nasm_error(ERR_NONFATAL, "32-bit ELF format does not support 64-bit relocations");
1535 addr = 0;
1536 elf_sect_writeaddr(s, addr, 8);
1537 break;
1541 static void elf_write(void)
1543 int align;
1544 char *p;
1545 int i;
1547 struct SAA *symtab;
1548 int32_t symtablen, symtablocal;
1551 * Work out how many sections we will have. We have SHN_UNDEF,
1552 * then the flexible user sections, then the fixed sections
1553 * `.shstrtab', `.symtab' and `.strtab', then optionally
1554 * relocation sections for the user sections.
1556 nsections = sec_numspecial + 1;
1557 if (dfmt_is_stabs())
1558 nsections += 3;
1559 else if (dfmt_is_dwarf())
1560 nsections += 10;
1562 add_sectname("", ".shstrtab");
1563 add_sectname("", ".symtab");
1564 add_sectname("", ".strtab");
1565 for (i = 0; i < nsects; i++) {
1566 nsections++; /* for the section itself */
1567 if (sects[i]->head) {
1568 nsections++; /* for its relocations */
1569 add_sectname(is_elf32() ? ".rel" : ".rela", sects[i]->name);
1573 if (dfmt_is_stabs()) {
1574 /* in case the debug information is wanted, just add these three sections... */
1575 add_sectname("", ".stab");
1576 add_sectname("", ".stabstr");
1577 add_sectname(is_elf32() ? ".rel" : ".rela", ".stab");
1578 } else if (dfmt_is_dwarf()) {
1579 /* the dwarf debug standard specifies the following ten sections,
1580 not all of which are currently implemented,
1581 although all of them are defined. */
1582 #define debug_aranges (int64_t) (nsections-10)
1583 #define debug_info (int64_t) (nsections-7)
1584 #define debug_abbrev (int64_t) (nsections-5)
1585 #define debug_line (int64_t) (nsections-4)
1586 add_sectname("", ".debug_aranges");
1587 add_sectname(".rela", ".debug_aranges");
1588 add_sectname("", ".debug_pubnames");
1589 add_sectname("", ".debug_info");
1590 add_sectname(".rela", ".debug_info");
1591 add_sectname("", ".debug_abbrev");
1592 add_sectname("", ".debug_line");
1593 add_sectname(".rela", ".debug_line");
1594 add_sectname("", ".debug_frame");
1595 add_sectname("", ".debug_loc");
1599 * Output the ELF header.
1601 if (is_elf32() || is_elfx32()) {
1602 nasm_write("\177ELF\1\1\1", 7, ofile);
1603 fputc(elf_osabi, ofile);
1604 fputc(elf_abiver, ofile);
1605 fwritezero(7, ofile);
1606 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1607 fwriteint16_t(is_elf32() ? EM_386 : EM_X86_64, ofile); /* processor ID */
1608 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1609 fwriteint32_t(0L, ofile); /* no entry point */
1610 fwriteint32_t(0L, ofile); /* no program header table */
1611 fwriteint32_t(0x40L, ofile); /* section headers straight after ELF header plus alignment */
1612 fwriteint32_t(0L, ofile); /* no special flags */
1613 fwriteint16_t(0x34, ofile); /* size of ELF header */
1614 fwriteint16_t(0, ofile); /* no program header table, again */
1615 fwriteint16_t(0, ofile); /* still no program header table */
1616 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
1617 fwriteint16_t(nsections, ofile); /* number of sections */
1618 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for section header table */
1620 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1621 fwriteint32_t(0L, ofile);
1622 fwriteint32_t(0L, ofile);
1623 } else {
1624 nasm_assert(is_elf64());
1625 nasm_write("\177ELF\2\1\1", 7, ofile);
1626 fputc(elf_osabi, ofile);
1627 fputc(elf_abiver, ofile);
1628 fwritezero(7, ofile);
1629 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1630 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1631 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1632 fwriteint64_t(0L, ofile); /* no entry point */
1633 fwriteint64_t(0L, ofile); /* no program header table */
1634 fwriteint64_t(0x40L, ofile); /* section headers straight after ELF header plus alignment */
1635 fwriteint32_t(0L, ofile); /* no special flags */
1636 fwriteint16_t(0x40, ofile); /* size of ELF header */
1637 fwriteint16_t(0, ofile); /* no program header table, again */
1638 fwriteint16_t(0, ofile); /* still no program header table */
1639 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1640 fwriteint16_t(nsections, ofile); /* number of sections */
1641 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for section header table */
1645 * Build the symbol table and relocation tables.
1647 symtab = elf_build_symtab(&symtablen, &symtablocal);
1648 for (i = 0; i < nsects; i++)
1649 if (sects[i]->head)
1650 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1651 sects[i]->head);
1654 * Now output the section header table.
1657 elf_foffs = 0x40 + (is_elf64() ? sizeof(Elf64_Shdr): sizeof(Elf32_Shdr)) * nsections;
1658 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1659 elf_foffs += align;
1660 elf_nsect = 0;
1661 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1663 /* SHN_UNDEF */
1664 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1665 p = shstrtab + 1;
1667 /* The normal sections */
1668 for (i = 0; i < nsects; i++) {
1669 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1670 (sects[i]->type == SHT_PROGBITS ?
1671 sects[i]->data : NULL), true,
1672 sects[i]->len, 0, 0, sects[i]->align, 0);
1673 p += strlen(p) + 1;
1676 /* .shstrtab */
1677 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1678 shstrtablen, 0, 0, 1, 0);
1679 p += strlen(p) + 1;
1681 /* .symtab */
1682 if (is_elf64())
1683 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1684 symtablen, sec_strtab, symtablocal, 8, 24);
1685 else
1686 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1687 symtablen, sec_strtab, symtablocal, 4, 16);
1688 p += strlen(p) + 1;
1690 /* .strtab */
1691 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1692 strslen, 0, 0, 1, 0);
1693 p += strlen(p) + 1;
1695 /* The relocation sections */
1696 if (is_elf32()) {
1697 for (i = 0; i < nsects; i++) {
1698 if (sects[i]->head) {
1699 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1700 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1701 p += strlen(p) + 1;
1704 } else if (is_elfx32()) {
1705 for (i = 0; i < nsects; i++) {
1706 if (sects[i]->head) {
1707 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1708 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1709 p += strlen(p) + 1;
1712 } else {
1713 nasm_assert(is_elf64());
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, 8, 24);
1718 p += strlen(p) + 1;
1723 if (dfmt_is_stabs()) {
1724 /* for debugging information, create the last three sections
1725 which are the .stab , .stabstr and .rel.stab sections respectively */
1727 /* this function call creates the stab sections in memory */
1728 stabs_generate();
1730 if (stabbuf && stabstrbuf && stabrelbuf) {
1731 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1732 stablen, sec_stabstr, 0, 4, 12);
1733 p += strlen(p) + 1;
1735 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1736 stabstrlen, 0, 0, 4, 0);
1737 p += strlen(p) + 1;
1739 /* link -> symtable info -> section to refer to */
1740 if (is_elf32()) {
1741 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1742 stabrellen, sec_symtab, sec_stab, 4, 8);
1743 } else {
1744 elf_section_header(p - shstrtab, SHT_RELA, 0, stabrelbuf, false,
1745 stabrellen, sec_symtab, sec_stab, 4, is_elf64() ? 24 : 12);
1747 p += strlen(p) + 1;
1749 } else if (dfmt_is_dwarf()) {
1750 /* for dwarf debugging information, create the ten dwarf sections */
1752 /* this function call creates the dwarf sections in memory */
1753 if (dwarf_fsect)
1754 dwarf_generate();
1756 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1757 arangeslen, 0, 0, 1, 0);
1758 p += strlen(p) + 1;
1760 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1761 arangesrellen, sec_symtab,
1762 is_elf64() ? debug_aranges : sec_debug_aranges,
1763 1, is_elf64() ? 24 : 12);
1764 p += strlen(p) + 1;
1766 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1767 false, pubnameslen, 0, 0, 1, 0);
1768 p += strlen(p) + 1;
1770 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1771 infolen, 0, 0, 1, 0);
1772 p += strlen(p) + 1;
1774 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1775 inforellen, sec_symtab,
1776 is_elf64() ? debug_info : sec_debug_info,
1777 1, is_elf64() ? 24 : 12);
1778 p += strlen(p) + 1;
1780 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1781 abbrevlen, 0, 0, 1, 0);
1782 p += strlen(p) + 1;
1784 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1785 linelen, 0, 0, 1, 0);
1786 p += strlen(p) + 1;
1788 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1789 linerellen, sec_symtab,
1790 is_elf64() ? debug_line : sec_debug_line,
1791 1, is_elf64() ? 24 : 12);
1792 p += strlen(p) + 1;
1794 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1795 framelen, 0, 0, 8, 0);
1796 p += strlen(p) + 1;
1798 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1799 loclen, 0, 0, 1, 0);
1800 p += strlen(p) + 1;
1802 fwritezero(align, ofile);
1805 * Now output the sections.
1807 elf_write_sections();
1809 nasm_free(elf_sects);
1810 saa_free(symtab);
1813 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1815 struct SAA *s = saa_init(1L);
1816 struct elf_symbol *sym;
1817 uint8_t entry[24], *p;
1818 int i;
1820 *len = *local = 0;
1823 * First, an all-zeros entry, required by the ELF spec.
1825 saa_wbytes(s, NULL, is_elf64() ? 24L : 16L); /* null symbol table entry */
1826 *len += is_elf64() ? 24L : 16L;
1827 (*local)++;
1830 * Next, an entry for the file name.
1832 p = entry;
1833 if (is_elf64()) {
1834 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1835 WRITESHORT(p, STT_FILE); /* type FILE */
1836 WRITESHORT(p, SHN_ABS);
1837 WRITEDLONG(p, (uint64_t) 0); /* no value */
1838 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1839 saa_wbytes(s, entry, 24L);
1840 *len += 24;
1841 (*local)++;
1842 } else {
1843 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1844 WRITELONG(p, 0); /* no value */
1845 WRITELONG(p, 0); /* no size either */
1846 WRITESHORT(p, STT_FILE); /* type FILE */
1847 WRITESHORT(p, SHN_ABS);
1848 saa_wbytes(s, entry, 16L);
1849 *len += 16;
1850 (*local)++;
1854 * Now some standard symbols defining the segments, for relocation
1855 * purposes.
1857 if (is_elf64()) {
1858 for (i = 1; i <= nsects; i++) {
1859 p = entry;
1860 WRITELONG(p, 0); /* no symbol name */
1861 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1862 WRITESHORT(p, i); /* section id */
1863 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1864 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1865 saa_wbytes(s, entry, 24L);
1866 *len += 24;
1867 (*local)++;
1869 } else {
1870 for (i = 1; i <= nsects; i++) {
1871 p = entry;
1872 WRITELONG(p, 0); /* no symbol name */
1873 WRITELONG(p, 0); /* offset zero */
1874 WRITELONG(p, 0); /* size zero */
1875 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1876 WRITESHORT(p, i); /* section id */
1877 saa_wbytes(s, entry, 16L);
1878 *len += 16;
1879 (*local)++;
1884 * Now the other local symbols.
1886 saa_rewind(syms);
1887 if (is_elf64()) {
1888 while ((sym = saa_rstruct(syms))) {
1889 if (sym->type & SYM_GLOBAL)
1890 continue;
1891 p = entry;
1892 WRITELONG(p, sym->strpos); /* index into symbol string table */
1893 WRITECHAR(p, sym->type); /* type and binding */
1894 WRITECHAR(p, sym->other); /* visibility */
1895 WRITESHORT(p, sym->section); /* index into section header table */
1896 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1897 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1898 saa_wbytes(s, entry, 24L);
1899 *len += 24;
1900 (*local)++;
1903 * dwarf needs symbols for debug sections
1904 * which are relocation targets.
1906 if (dfmt_is_dwarf()) {
1907 dwarf_infosym = *local;
1908 p = entry;
1909 WRITELONG(p, 0); /* no symbol name */
1910 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1911 WRITESHORT(p, debug_info); /* section id */
1912 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1913 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1914 saa_wbytes(s, entry, 24L);
1915 *len += 24;
1916 (*local)++;
1917 dwarf_abbrevsym = *local;
1918 p = entry;
1919 WRITELONG(p, 0); /* no symbol name */
1920 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1921 WRITESHORT(p, debug_abbrev); /* section id */
1922 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1923 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1924 saa_wbytes(s, entry, 24L);
1925 *len += 24;
1926 (*local)++;
1927 dwarf_linesym = *local;
1928 p = entry;
1929 WRITELONG(p, 0); /* no symbol name */
1930 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1931 WRITESHORT(p, debug_line); /* section id */
1932 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1933 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1934 saa_wbytes(s, entry, 24L);
1935 *len += 24;
1936 (*local)++;
1938 } else {
1939 while ((sym = saa_rstruct(syms))) {
1940 if (sym->type & SYM_GLOBAL)
1941 continue;
1942 p = entry;
1943 WRITELONG(p, sym->strpos);
1944 WRITELONG(p, sym->symv.key);
1945 WRITELONG(p, sym->size);
1946 WRITECHAR(p, sym->type); /* type and binding */
1947 WRITECHAR(p, sym->other); /* visibility */
1948 WRITESHORT(p, sym->section);
1949 saa_wbytes(s, entry, 16L);
1950 *len += 16;
1951 (*local)++;
1954 * dwarf needs symbols for debug sections
1955 * which are relocation targets.
1957 if (dfmt_is_dwarf()) {
1958 dwarf_infosym = *local;
1959 p = entry;
1960 WRITELONG(p, 0); /* no symbol name */
1961 WRITELONG(p, (uint32_t) 0); /* offset zero */
1962 WRITELONG(p, (uint32_t) 0); /* size zero */
1963 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1964 WRITESHORT(p, sec_debug_info); /* section id */
1965 saa_wbytes(s, entry, 16L);
1966 *len += 16;
1967 (*local)++;
1968 dwarf_abbrevsym = *local;
1969 p = entry;
1970 WRITELONG(p, 0); /* no symbol name */
1971 WRITELONG(p, (uint32_t) 0); /* offset zero */
1972 WRITELONG(p, (uint32_t) 0); /* size zero */
1973 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1974 WRITESHORT(p, sec_debug_abbrev); /* section id */
1975 saa_wbytes(s, entry, 16L);
1976 *len += 16;
1977 (*local)++;
1978 dwarf_linesym = *local;
1979 p = entry;
1980 WRITELONG(p, 0); /* no symbol name */
1981 WRITELONG(p, (uint32_t) 0); /* offset zero */
1982 WRITELONG(p, (uint32_t) 0); /* size zero */
1983 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1984 WRITESHORT(p, sec_debug_line); /* section id */
1985 saa_wbytes(s, entry, 16L);
1986 *len += 16;
1987 (*local)++;
1992 * Now the global symbols.
1994 saa_rewind(syms);
1995 if (is_elf64()) {
1996 while ((sym = saa_rstruct(syms))) {
1997 if (!(sym->type & SYM_GLOBAL))
1998 continue;
1999 p = entry;
2000 WRITELONG(p, sym->strpos);
2001 WRITECHAR(p, sym->type); /* type and binding */
2002 WRITECHAR(p, sym->other); /* visibility */
2003 WRITESHORT(p, sym->section);
2004 WRITEDLONG(p, (int64_t)sym->symv.key);
2005 WRITEDLONG(p, (int64_t)sym->size);
2006 saa_wbytes(s, entry, 24L);
2007 *len += 24;
2009 } else {
2010 while ((sym = saa_rstruct(syms))) {
2011 if (!(sym->type & SYM_GLOBAL))
2012 continue;
2013 p = entry;
2014 WRITELONG(p, sym->strpos);
2015 WRITELONG(p, sym->symv.key);
2016 WRITELONG(p, sym->size);
2017 WRITECHAR(p, sym->type); /* type and binding */
2018 WRITECHAR(p, sym->other); /* visibility */
2019 WRITESHORT(p, sym->section);
2020 saa_wbytes(s, entry, 16L);
2021 *len += 16;
2025 return s;
2028 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
2030 struct SAA *s;
2031 uint8_t *p, entry[24];
2032 int32_t global_offset;
2034 if (!r)
2035 return NULL;
2037 s = saa_init(1L);
2038 *len = 0;
2041 * How to onvert from a global placeholder to a real symbol index;
2042 * the +2 refers to the two special entries, the null entry and
2043 * the filename entry.
2045 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
2047 if (is_elf32()) {
2048 while (r) {
2049 int32_t sym = r->symbol;
2051 if (sym >= GLOBAL_TEMP_BASE)
2052 sym += global_offset;
2054 p = entry;
2055 WRITELONG(p, r->address);
2056 WRITELONG(p, (sym << 8) + r->type);
2057 saa_wbytes(s, entry, 8L);
2058 *len += 8;
2060 r = r->next;
2062 } else if (is_elfx32()) {
2063 while (r) {
2064 int32_t sym = r->symbol;
2066 if (sym >= GLOBAL_TEMP_BASE)
2067 sym += global_offset;
2069 p = entry;
2070 WRITELONG(p, r->address);
2071 WRITELONG(p, (sym << 8) + r->type);
2072 WRITELONG(p, r->offset);
2073 saa_wbytes(s, entry, 12L);
2074 *len += 12;
2076 r = r->next;
2078 } else {
2079 nasm_assert(is_elf64());
2080 while (r) {
2081 int32_t sym = r->symbol;
2083 if (sym >= GLOBAL_TEMP_BASE)
2084 sym += global_offset;
2086 p = entry;
2087 WRITEDLONG(p, r->address);
2088 WRITELONG(p, r->type);
2089 WRITELONG(p, sym);
2090 WRITEDLONG(p, r->offset);
2091 saa_wbytes(s, entry, 24L);
2092 *len += 24;
2094 r = r->next;
2098 return s;
2101 static void elf_section_header(int name, int type, uint64_t flags,
2102 void *data, bool is_saa, uint64_t datalen,
2103 int link, int info, int align, int eltsize)
2105 elf_sects[elf_nsect].data = data;
2106 elf_sects[elf_nsect].len = datalen;
2107 elf_sects[elf_nsect].is_saa = is_saa;
2108 elf_nsect++;
2110 if (is_elf32()) {
2111 fwriteint32_t((int32_t)name, ofile);
2112 fwriteint32_t((int32_t)type, ofile);
2113 fwriteint32_t((int32_t)flags, ofile);
2114 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
2115 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
2116 fwriteint32_t(datalen, ofile);
2117 if (data)
2118 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2119 fwriteint32_t((int32_t)link, ofile);
2120 fwriteint32_t((int32_t)info, ofile);
2121 fwriteint32_t((int32_t)align, ofile);
2122 fwriteint32_t((int32_t)eltsize, ofile);
2123 } else if (is_elfx32()) {
2124 fwriteint32_t((int32_t)name, ofile);
2125 fwriteint32_t((int32_t)type, ofile);
2126 fwriteint32_t((int32_t)flags, ofile);
2127 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
2128 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
2129 fwriteint32_t(datalen, ofile);
2130 if (data)
2131 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2132 fwriteint32_t((int32_t)link, ofile);
2133 fwriteint32_t((int32_t)info, ofile);
2134 fwriteint32_t((int32_t)align, ofile);
2135 fwriteint32_t((int32_t)eltsize, ofile);
2136 } else {
2137 nasm_assert(is_elf64());
2138 fwriteint32_t((int32_t)name, ofile);
2139 fwriteint32_t((int32_t)type, ofile);
2140 fwriteint64_t((int64_t)flags, ofile);
2141 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
2142 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
2143 fwriteint64_t(datalen, ofile);
2144 if (data)
2145 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2146 fwriteint32_t((int32_t)link, ofile);
2147 fwriteint32_t((int32_t)info, ofile);
2148 fwriteint64_t((int64_t)align, ofile);
2149 fwriteint64_t((int64_t)eltsize, ofile);
2153 static void elf_write_sections(void)
2155 int i;
2156 for (i = 0; i < elf_nsect; i++)
2157 if (elf_sects[i].data) {
2158 int32_t len = elf_sects[i].len;
2159 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
2160 int32_t align = reallen - len;
2161 if (elf_sects[i].is_saa)
2162 saa_fpwrite(elf_sects[i].data, ofile);
2163 else
2164 nasm_write(elf_sects[i].data, len, ofile);
2165 fwritezero(align, ofile);
2169 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
2171 saa_wbytes(sect->data, data, len);
2172 sect->len += len;
2175 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
2177 saa_writeaddr(sect->data, data, len);
2178 sect->len += len;
2181 static void elf_sectalign(int32_t seg, unsigned int value)
2183 struct elf_section *s = NULL;
2184 int i;
2186 for (i = 0; i < nsects; i++) {
2187 if (sects[i]->index == seg) {
2188 s = sects[i];
2189 break;
2192 if (!s || !is_power2(value))
2193 return;
2195 if (value > s->align)
2196 s->align = value;
2199 static int32_t elf_segbase(int32_t segment)
2201 return segment;
2204 static void elf_filename(char *inname, char *outname)
2206 strcpy(elf_module, inname);
2207 standard_extension(inname, outname, ".o");
2210 extern macros_t elf_stdmac[];
2212 static int elf_set_info(enum geninfo type, char **val)
2214 (void)type;
2215 (void)val;
2216 return 0;
2219 static const struct dfmt elf32_df_dwarf = {
2220 "ELF32 (i386) dwarf debug format for Linux/Unix",
2221 "dwarf",
2222 dwarf_init,
2223 dwarf_linenum,
2224 null_debug_deflabel,
2225 null_debug_directive,
2226 debug_typevalue,
2227 dwarf_output,
2228 dwarf_cleanup
2231 static const struct dfmt elf32_df_stabs = {
2232 "ELF32 (i386) stabs debug format for Linux/Unix",
2233 "stabs",
2234 null_debug_init,
2235 stabs_linenum,
2236 null_debug_deflabel,
2237 null_debug_directive,
2238 debug_typevalue,
2239 stabs_output,
2240 stabs_cleanup
2243 static const struct dfmt * const elf32_debugs_arr[3] =
2244 { &elf32_df_dwarf, &elf32_df_stabs, NULL };
2246 const struct ofmt of_elf32 = {
2247 "ELF32 (i386) object files (e.g. Linux)",
2248 "elf32",
2251 elf32_debugs_arr,
2252 &elf32_df_stabs,
2253 elf_stdmac,
2254 elf_init,
2255 elf_set_info,
2256 elf32_out,
2257 elf_deflabel,
2258 elf_section_names,
2259 elf_sectalign,
2260 elf_segbase,
2261 elf_directive,
2262 elf_filename,
2263 elf_cleanup
2266 static const struct dfmt elf64_df_dwarf = {
2267 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
2268 "dwarf",
2269 dwarf_init,
2270 dwarf_linenum,
2271 null_debug_deflabel,
2272 null_debug_directive,
2273 debug_typevalue,
2274 dwarf_output,
2275 dwarf_cleanup
2278 static const struct dfmt elf64_df_stabs = {
2279 "ELF64 (x86-64) stabs debug format for Linux/Unix",
2280 "stabs",
2281 null_debug_init,
2282 stabs_linenum,
2283 null_debug_deflabel,
2284 null_debug_directive,
2285 debug_typevalue,
2286 stabs_output,
2287 stabs_cleanup
2290 static const struct dfmt * const elf64_debugs_arr[3] =
2291 { &elf64_df_dwarf, &elf64_df_stabs, NULL };
2293 const struct ofmt of_elf64 = {
2294 "ELF64 (x86_64) object files (e.g. Linux)",
2295 "elf64",
2298 elf64_debugs_arr,
2299 &elf64_df_stabs,
2300 elf_stdmac,
2301 elf_init,
2302 elf_set_info,
2303 elf64_out,
2304 elf_deflabel,
2305 elf_section_names,
2306 elf_sectalign,
2307 elf_segbase,
2308 elf_directive,
2309 elf_filename,
2310 elf_cleanup
2313 static const struct dfmt elfx32_df_dwarf = {
2314 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
2315 "dwarf",
2316 dwarf_init,
2317 dwarf_linenum,
2318 null_debug_deflabel,
2319 null_debug_directive,
2320 debug_typevalue,
2321 dwarf_output,
2322 dwarf_cleanup
2325 static const struct dfmt elfx32_df_stabs = {
2326 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
2327 "stabs",
2328 null_debug_init,
2329 stabs_linenum,
2330 null_debug_deflabel,
2331 null_debug_directive,
2332 debug_typevalue,
2333 stabs_output,
2334 stabs_cleanup
2337 static const struct dfmt * const elfx32_debugs_arr[3] =
2338 { &elfx32_df_dwarf, &elfx32_df_stabs, NULL };
2340 const struct ofmt of_elfx32 = {
2341 "ELFX32 (x86_64) object files (e.g. Linux)",
2342 "elfx32",
2345 elfx32_debugs_arr,
2346 &elfx32_df_stabs,
2347 elf_stdmac,
2348 elf_init,
2349 elf_set_info,
2350 elfx32_out,
2351 elf_deflabel,
2352 elf_section_names,
2353 elf_sectalign,
2354 elf_segbase,
2355 elf_directive,
2356 elf_filename,
2357 elf_cleanup
2360 static bool is_elf64(void)
2362 return ofmt == &of_elf64;
2365 static bool is_elf32(void)
2367 return ofmt == &of_elf32;
2370 static bool is_elfx32(void)
2372 return ofmt == &of_elfx32;
2375 static bool dfmt_is_stabs(void)
2377 return dfmt == &elf32_df_stabs || dfmt == &elfx32_df_stabs || dfmt == &elf64_df_stabs;
2380 static bool dfmt_is_dwarf(void)
2382 return dfmt == &elf32_df_dwarf || dfmt == &elfx32_df_dwarf || dfmt == &elf64_df_dwarf;
2385 /* common debugging routines */
2386 static void debug_typevalue(int32_t type)
2388 int32_t stype, ssize;
2389 switch (TYM_TYPE(type)) {
2390 case TY_LABEL:
2391 ssize = 0;
2392 stype = STT_NOTYPE;
2393 break;
2394 case TY_BYTE:
2395 ssize = 1;
2396 stype = STT_OBJECT;
2397 break;
2398 case TY_WORD:
2399 ssize = 2;
2400 stype = STT_OBJECT;
2401 break;
2402 case TY_DWORD:
2403 ssize = 4;
2404 stype = STT_OBJECT;
2405 break;
2406 case TY_FLOAT:
2407 ssize = 4;
2408 stype = STT_OBJECT;
2409 break;
2410 case TY_QWORD:
2411 ssize = 8;
2412 stype = STT_OBJECT;
2413 break;
2414 case TY_TBYTE:
2415 ssize = 10;
2416 stype = STT_OBJECT;
2417 break;
2418 case TY_OWORD:
2419 ssize = 16;
2420 stype = STT_OBJECT;
2421 break;
2422 case TY_YWORD:
2423 ssize = 32;
2424 stype = STT_OBJECT;
2425 break;
2426 case TY_COMMON:
2427 ssize = 0;
2428 stype = STT_COMMON;
2429 break;
2430 case TY_SEG:
2431 ssize = 0;
2432 stype = STT_SECTION;
2433 break;
2434 case TY_EXTERN:
2435 ssize = 0;
2436 stype = STT_NOTYPE;
2437 break;
2438 case TY_EQU:
2439 ssize = 0;
2440 stype = STT_NOTYPE;
2441 break;
2442 default:
2443 ssize = 0;
2444 stype = STT_NOTYPE;
2445 break;
2447 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
2448 lastsym->size = ssize;
2449 lastsym->type = stype;
2453 /* stabs debugging routines */
2455 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t segto)
2457 (void)segto;
2458 if (!stabs_filename) {
2459 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
2460 strcpy(stabs_filename, filename);
2461 } else {
2462 if (strcmp(stabs_filename, filename)) {
2463 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2464 in fact, this leak comes in quite handy to maintain a list of files
2465 encountered so far in the symbol lines... */
2467 /* why not nasm_free(stabs_filename); we're done with the old one */
2469 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
2470 strcpy(stabs_filename, filename);
2473 debug_immcall = 1;
2474 currentline = linenumber;
2477 static void stabs_output(int type, void *param)
2479 struct symlininfo *s;
2480 struct linelist *el;
2481 if (type == TY_DEBUGSYMLIN) {
2482 if (debug_immcall) {
2483 s = (struct symlininfo *)param;
2484 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2485 return; /* line info is only collected for executable sections */
2486 numlinestabs++;
2487 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2488 el->info.offset = s->offset;
2489 el->info.section = s->section;
2490 el->info.name = s->name;
2491 el->line = currentline;
2492 el->filename = stabs_filename;
2493 el->next = 0;
2494 if (stabslines) {
2495 stabslines->last->next = el;
2496 stabslines->last = el;
2497 } else {
2498 stabslines = el;
2499 stabslines->last = el;
2503 debug_immcall = 0;
2506 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2508 static void stabs_generate(void)
2510 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
2511 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
2512 char **allfiles;
2513 int *fileidx;
2515 struct linelist *ptr;
2517 ptr = stabslines;
2519 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
2520 numfiles = 0;
2521 while (ptr) {
2522 if (numfiles == 0) {
2523 allfiles[0] = ptr->filename;
2524 numfiles++;
2525 } else {
2526 for (i = 0; i < numfiles; i++) {
2527 if (!strcmp(allfiles[i], ptr->filename))
2528 break;
2530 if (i >= numfiles) {
2531 allfiles[i] = ptr->filename;
2532 numfiles++;
2535 ptr = ptr->next;
2537 strsize = 1;
2538 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
2539 for (i = 0; i < numfiles; i++) {
2540 fileidx[i] = strsize;
2541 strsize += strlen(allfiles[i]) + 1;
2543 mainfileindex = 0;
2544 for (i = 0; i < numfiles; i++) {
2545 if (!strcmp(allfiles[i], elf_module)) {
2546 mainfileindex = i;
2547 break;
2552 * worst case size of the stab buffer would be:
2553 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2554 * plus one "ending" entry
2556 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
2557 sizeof(struct stabentry));
2558 ssbuf = (uint8_t *)nasm_malloc(strsize);
2559 rbuf = (uint8_t *)nasm_malloc(numlinestabs * (is_elf64() ? 16 : 8) * (2 + 3));
2560 rptr = rbuf;
2562 for (i = 0; i < numfiles; i++)
2563 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
2564 ssbuf[0] = 0;
2566 stabstrlen = strsize; /* set global variable for length of stab strings */
2568 sptr = sbuf;
2569 ptr = stabslines;
2570 numstabs = 0;
2572 if (ptr) {
2574 * this is the first stab, its strx points to the filename of the
2575 * the source-file, the n_desc field should be set to the number
2576 * of remaining stabs
2578 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
2580 /* this is the stab for the main source file */
2581 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
2583 /* relocation table entry */
2586 * Since the symbol table has two entries before
2587 * the section symbols, the index in the info.section
2588 * member must be adjusted by adding 2
2591 if (is_elf32()) {
2592 WRITELONG(rptr, (sptr - sbuf) - 4);
2593 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2594 } else if (is_elfx32()) {
2595 WRITELONG(rptr, (sptr - sbuf) - 4);
2596 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2597 WRITELONG(rptr, 0);
2598 } else {
2599 nasm_assert(is_elf64());
2600 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2601 WRITELONG(rptr, R_X86_64_32);
2602 WRITELONG(rptr, ptr->info.section + 2);
2603 WRITEDLONG(rptr, 0);
2605 numstabs++;
2606 currfile = mainfileindex;
2609 if (is_elf32()) {
2610 while (ptr) {
2611 if (strcmp(allfiles[currfile], ptr->filename)) {
2612 /* oops file has changed... */
2613 for (i = 0; i < numfiles; i++)
2614 if (!strcmp(allfiles[i], ptr->filename))
2615 break;
2616 currfile = i;
2617 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2618 ptr->info.offset);
2619 numstabs++;
2621 /* relocation table entry */
2622 WRITELONG(rptr, (sptr - sbuf) - 4);
2623 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2626 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2627 numstabs++;
2629 /* relocation table entry */
2630 WRITELONG(rptr, (sptr - sbuf) - 4);
2631 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2633 ptr = ptr->next;
2635 } else if (is_elfx32()) {
2636 while (ptr) {
2637 if (strcmp(allfiles[currfile], ptr->filename)) {
2638 /* oops file has changed... */
2639 for (i = 0; i < numfiles; i++)
2640 if (!strcmp(allfiles[i], ptr->filename))
2641 break;
2642 currfile = i;
2643 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2644 ptr->info.offset);
2645 numstabs++;
2647 /* relocation table entry */
2648 WRITELONG(rptr, (sptr - sbuf) - 4);
2649 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2650 WRITELONG(rptr, ptr->info.offset);
2653 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2654 numstabs++;
2656 /* relocation table entry */
2657 WRITELONG(rptr, (sptr - sbuf) - 4);
2658 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2659 WRITELONG(rptr, ptr->info.offset);
2661 ptr = ptr->next;
2663 } else {
2664 nasm_assert(is_elf64());
2665 while (ptr) {
2666 if (strcmp(allfiles[currfile], ptr->filename)) {
2667 /* oops file has changed... */
2668 for (i = 0; i < numfiles; i++)
2669 if (!strcmp(allfiles[i], ptr->filename))
2670 break;
2671 currfile = i;
2672 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2673 ptr->info.offset);
2674 numstabs++;
2676 /* relocation table entry */
2677 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2678 WRITELONG(rptr, R_X86_64_32);
2679 WRITELONG(rptr, ptr->info.section + 2);
2680 WRITEDLONG(rptr, ptr->info.offset);
2683 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2684 numstabs++;
2686 /* relocation table entry */
2687 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2688 WRITELONG(rptr, R_X86_64_32);
2689 WRITELONG(rptr, ptr->info.section + 2);
2690 WRITEDLONG(rptr, ptr->info.offset);
2692 ptr = ptr->next;
2696 /* this is an "ending" token */
2697 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
2698 numstabs++;
2700 ((struct stabentry *)sbuf)->n_desc = numstabs;
2702 nasm_free(allfiles);
2703 nasm_free(fileidx);
2705 stablen = (sptr - sbuf);
2706 stabrellen = (rptr - rbuf);
2707 stabrelbuf = rbuf;
2708 stabbuf = sbuf;
2709 stabstrbuf = ssbuf;
2712 static void stabs_cleanup(void)
2714 struct linelist *ptr, *del;
2715 if (!stabslines)
2716 return;
2718 ptr = stabslines;
2719 while (ptr) {
2720 del = ptr;
2721 ptr = ptr->next;
2722 nasm_free(del);
2725 nasm_free(stabbuf);
2726 nasm_free(stabrelbuf);
2727 nasm_free(stabstrbuf);
2730 /* dwarf routines */
2732 static void dwarf_init(void)
2734 ndebugs = 3; /* 3 debug symbols */
2737 static void dwarf_linenum(const char *filename, int32_t linenumber,
2738 int32_t segto)
2740 (void)segto;
2741 dwarf_findfile(filename);
2742 debug_immcall = 1;
2743 currentline = linenumber;
2746 /* called from elf_out with type == TY_DEBUGSYMLIN */
2747 static void dwarf_output(int type, void *param)
2749 int ln, aa, inx, maxln, soc;
2750 struct symlininfo *s;
2751 struct SAA *plinep;
2753 (void)type;
2755 s = (struct symlininfo *)param;
2757 /* line number info is only gathered for executable sections */
2758 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2759 return;
2761 /* Check if section index has changed */
2762 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
2763 dwarf_findsect(s->section);
2765 /* do nothing unless line or file has changed */
2766 if (!debug_immcall)
2767 return;
2769 ln = currentline - dwarf_csect->line;
2770 aa = s->offset - dwarf_csect->offset;
2771 inx = dwarf_clist->line;
2772 plinep = dwarf_csect->psaa;
2773 /* check for file change */
2774 if (!(inx == dwarf_csect->file)) {
2775 saa_write8(plinep,DW_LNS_set_file);
2776 saa_write8(plinep,inx);
2777 dwarf_csect->file = inx;
2779 /* check for line change */
2780 if (ln) {
2781 /* test if in range of special op code */
2782 maxln = line_base + line_range;
2783 soc = (ln - line_base) + (line_range * aa) + opcode_base;
2784 if (ln >= line_base && ln < maxln && soc < 256) {
2785 saa_write8(plinep,soc);
2786 } else {
2787 saa_write8(plinep,DW_LNS_advance_line);
2788 saa_wleb128s(plinep,ln);
2789 if (aa) {
2790 saa_write8(plinep,DW_LNS_advance_pc);
2791 saa_wleb128u(plinep,aa);
2794 dwarf_csect->line = currentline;
2795 dwarf_csect->offset = s->offset;
2798 /* show change handled */
2799 debug_immcall = 0;
2803 static void dwarf_generate(void)
2805 uint8_t *pbuf;
2806 int indx;
2807 struct linelist *ftentry;
2808 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2809 struct SAA *parangesrel, *plinesrel, *pinforel;
2810 struct sectlist *psect;
2811 size_t saalen, linepoff, totlen, highaddr;
2813 if (is_elf32()) {
2814 /* write epilogues for each line program range */
2815 /* and build aranges section */
2816 paranges = saa_init(1L);
2817 parangesrel = saa_init(1L);
2818 saa_write16(paranges,2); /* dwarf version */
2819 saa_write32(parangesrel, paranges->datalen+4);
2820 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
2821 saa_write32(parangesrel, 0);
2822 saa_write32(paranges,0); /* offset into info */
2823 saa_write8(paranges,4); /* pointer size */
2824 saa_write8(paranges,0); /* not segmented */
2825 saa_write32(paranges,0); /* padding */
2826 /* iterate though sectlist entries */
2827 psect = dwarf_fsect;
2828 totlen = 0;
2829 highaddr = 0;
2830 for (indx = 0; indx < dwarf_nsections; indx++) {
2831 plinep = psect->psaa;
2832 /* Line Number Program Epilogue */
2833 saa_write8(plinep,2); /* std op 2 */
2834 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2835 saa_write8(plinep,DW_LNS_extended_op);
2836 saa_write8(plinep,1); /* operand length */
2837 saa_write8(plinep,DW_LNE_end_sequence);
2838 totlen += plinep->datalen;
2839 /* range table relocation entry */
2840 saa_write32(parangesrel, paranges->datalen + 4);
2841 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2842 saa_write32(parangesrel, (uint32_t) 0);
2843 /* range table entry */
2844 saa_write32(paranges,0x0000); /* range start */
2845 saa_write32(paranges,sects[psect->section]->len); /* range length */
2846 highaddr += sects[psect->section]->len;
2847 /* done with this entry */
2848 psect = psect->next;
2850 saa_write32(paranges,0); /* null address */
2851 saa_write32(paranges,0); /* null length */
2852 saalen = paranges->datalen;
2853 arangeslen = saalen + 4;
2854 arangesbuf = pbuf = nasm_malloc(arangeslen);
2855 WRITELONG(pbuf,saalen); /* initial length */
2856 saa_rnbytes(paranges, pbuf, saalen);
2857 saa_free(paranges);
2858 } else if (is_elfx32()) {
2859 /* write epilogues for each line program range */
2860 /* and build aranges section */
2861 paranges = saa_init(1L);
2862 parangesrel = saa_init(1L);
2863 saa_write16(paranges,3); /* dwarf version */
2864 saa_write32(parangesrel, paranges->datalen+4);
2865 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
2866 saa_write32(parangesrel, 0);
2867 saa_write32(paranges,0); /* offset into info */
2868 saa_write8(paranges,4); /* pointer size */
2869 saa_write8(paranges,0); /* not segmented */
2870 saa_write32(paranges,0); /* padding */
2871 /* iterate though sectlist entries */
2872 psect = dwarf_fsect;
2873 totlen = 0;
2874 highaddr = 0;
2875 for (indx = 0; indx < dwarf_nsections; indx++) {
2876 plinep = psect->psaa;
2877 /* Line Number Program Epilogue */
2878 saa_write8(plinep,2); /* std op 2 */
2879 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2880 saa_write8(plinep,DW_LNS_extended_op);
2881 saa_write8(plinep,1); /* operand length */
2882 saa_write8(plinep,DW_LNE_end_sequence);
2883 totlen += plinep->datalen;
2884 /* range table relocation entry */
2885 saa_write32(parangesrel, paranges->datalen + 4);
2886 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
2887 saa_write32(parangesrel, (uint32_t) 0);
2888 /* range table entry */
2889 saa_write32(paranges,0x0000); /* range start */
2890 saa_write32(paranges,sects[psect->section]->len); /* range length */
2891 highaddr += sects[psect->section]->len;
2892 /* done with this entry */
2893 psect = psect->next;
2895 saa_write32(paranges,0); /* null address */
2896 saa_write32(paranges,0); /* null length */
2897 saalen = paranges->datalen;
2898 arangeslen = saalen + 4;
2899 arangesbuf = pbuf = nasm_malloc(arangeslen);
2900 WRITELONG(pbuf,saalen); /* initial length */
2901 saa_rnbytes(paranges, pbuf, saalen);
2902 saa_free(paranges);
2903 } else {
2904 nasm_assert(is_elf64());
2905 /* write epilogues for each line program range */
2906 /* and build aranges section */
2907 paranges = saa_init(1L);
2908 parangesrel = saa_init(1L);
2909 saa_write16(paranges,3); /* dwarf version */
2910 saa_write64(parangesrel, paranges->datalen+4);
2911 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2912 saa_write64(parangesrel, 0);
2913 saa_write32(paranges,0); /* offset into info */
2914 saa_write8(paranges,8); /* pointer size */
2915 saa_write8(paranges,0); /* not segmented */
2916 saa_write32(paranges,0); /* padding */
2917 /* iterate though sectlist entries */
2918 psect = dwarf_fsect;
2919 totlen = 0;
2920 highaddr = 0;
2921 for (indx = 0; indx < dwarf_nsections; indx++) {
2922 plinep = psect->psaa;
2923 /* Line Number Program Epilogue */
2924 saa_write8(plinep,2); /* std op 2 */
2925 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2926 saa_write8(plinep,DW_LNS_extended_op);
2927 saa_write8(plinep,1); /* operand length */
2928 saa_write8(plinep,DW_LNE_end_sequence);
2929 totlen += plinep->datalen;
2930 /* range table relocation entry */
2931 saa_write64(parangesrel, paranges->datalen + 4);
2932 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2933 saa_write64(parangesrel, (uint64_t) 0);
2934 /* range table entry */
2935 saa_write64(paranges,0x0000); /* range start */
2936 saa_write64(paranges,sects[psect->section]->len); /* range length */
2937 highaddr += sects[psect->section]->len;
2938 /* done with this entry */
2939 psect = psect->next;
2941 saa_write64(paranges,0); /* null address */
2942 saa_write64(paranges,0); /* null length */
2943 saalen = paranges->datalen;
2944 arangeslen = saalen + 4;
2945 arangesbuf = pbuf = nasm_malloc(arangeslen);
2946 WRITELONG(pbuf,saalen); /* initial length */
2947 saa_rnbytes(paranges, pbuf, saalen);
2948 saa_free(paranges);
2951 /* build rela.aranges section */
2952 arangesrellen = saalen = parangesrel->datalen;
2953 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2954 saa_rnbytes(parangesrel, pbuf, saalen);
2955 saa_free(parangesrel);
2957 /* build pubnames section */
2958 ppubnames = saa_init(1L);
2959 saa_write16(ppubnames,3); /* dwarf version */
2960 saa_write32(ppubnames,0); /* offset into info */
2961 saa_write32(ppubnames,0); /* space used in info */
2962 saa_write32(ppubnames,0); /* end of list */
2963 saalen = ppubnames->datalen;
2964 pubnameslen = saalen + 4;
2965 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2966 WRITELONG(pbuf,saalen); /* initial length */
2967 saa_rnbytes(ppubnames, pbuf, saalen);
2968 saa_free(ppubnames);
2970 if (is_elf32()) {
2971 /* build info section */
2972 pinfo = saa_init(1L);
2973 pinforel = saa_init(1L);
2974 saa_write16(pinfo,2); /* dwarf version */
2975 saa_write32(pinforel, pinfo->datalen + 4);
2976 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
2977 saa_write32(pinforel, 0);
2978 saa_write32(pinfo,0); /* offset into abbrev */
2979 saa_write8(pinfo,4); /* pointer size */
2980 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2981 saa_write32(pinforel, pinfo->datalen + 4);
2982 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2983 saa_write32(pinforel, 0);
2984 saa_write32(pinfo,0); /* DW_AT_low_pc */
2985 saa_write32(pinforel, pinfo->datalen + 4);
2986 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2987 saa_write32(pinforel, 0);
2988 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
2989 saa_write32(pinforel, pinfo->datalen + 4);
2990 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
2991 saa_write32(pinforel, 0);
2992 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2993 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2994 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2995 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2996 saa_write8(pinfo,2); /* abbrviation number LEB128u */
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,0); /* DW_AT_low_pc */
3001 saa_write32(pinfo,0); /* DW_AT_frame_base */
3002 saa_write8(pinfo,0); /* end of entries */
3003 saalen = pinfo->datalen;
3004 infolen = saalen + 4;
3005 infobuf = pbuf = nasm_malloc(infolen);
3006 WRITELONG(pbuf,saalen); /* initial length */
3007 saa_rnbytes(pinfo, pbuf, saalen);
3008 saa_free(pinfo);
3009 } else if (is_elfx32()) {
3010 /* build info section */
3011 pinfo = saa_init(1L);
3012 pinforel = saa_init(1L);
3013 saa_write16(pinfo,3); /* dwarf version */
3014 saa_write32(pinforel, pinfo->datalen + 4);
3015 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
3016 saa_write32(pinforel, 0);
3017 saa_write32(pinfo,0); /* offset into abbrev */
3018 saa_write8(pinfo,4); /* pointer size */
3019 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3020 saa_write32(pinforel, pinfo->datalen + 4);
3021 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3022 saa_write32(pinforel, 0);
3023 saa_write32(pinfo,0); /* DW_AT_low_pc */
3024 saa_write32(pinforel, pinfo->datalen + 4);
3025 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3026 saa_write32(pinforel, 0);
3027 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
3028 saa_write32(pinforel, pinfo->datalen + 4);
3029 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
3030 saa_write32(pinforel, 0);
3031 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3032 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3033 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3034 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3035 saa_write8(pinfo,2); /* abbrviation number LEB128u */
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,0); /* DW_AT_low_pc */
3040 saa_write32(pinfo,0); /* DW_AT_frame_base */
3041 saa_write8(pinfo,0); /* end of entries */
3042 saalen = pinfo->datalen;
3043 infolen = saalen + 4;
3044 infobuf = pbuf = nasm_malloc(infolen);
3045 WRITELONG(pbuf,saalen); /* initial length */
3046 saa_rnbytes(pinfo, pbuf, saalen);
3047 saa_free(pinfo);
3048 } else {
3049 nasm_assert(is_elf64());
3050 /* build info section */
3051 pinfo = saa_init(1L);
3052 pinforel = saa_init(1L);
3053 saa_write16(pinfo,3); /* dwarf version */
3054 saa_write64(pinforel, pinfo->datalen + 4);
3055 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
3056 saa_write64(pinforel, 0);
3057 saa_write32(pinfo,0); /* offset into abbrev */
3058 saa_write8(pinfo,8); /* pointer size */
3059 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3060 saa_write64(pinforel, pinfo->datalen + 4);
3061 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3062 saa_write64(pinforel, 0);
3063 saa_write64(pinfo,0); /* DW_AT_low_pc */
3064 saa_write64(pinforel, pinfo->datalen + 4);
3065 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3066 saa_write64(pinforel, 0);
3067 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
3068 saa_write64(pinforel, pinfo->datalen + 4);
3069 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
3070 saa_write64(pinforel, 0);
3071 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3072 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3073 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3074 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3075 saa_write8(pinfo,2); /* abbrviation number LEB128u */
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,0); /* DW_AT_low_pc */
3080 saa_write64(pinfo,0); /* DW_AT_frame_base */
3081 saa_write8(pinfo,0); /* end of entries */
3082 saalen = pinfo->datalen;
3083 infolen = saalen + 4;
3084 infobuf = pbuf = nasm_malloc(infolen);
3085 WRITELONG(pbuf,saalen); /* initial length */
3086 saa_rnbytes(pinfo, pbuf, saalen);
3087 saa_free(pinfo);
3090 /* build rela.info section */
3091 inforellen = saalen = pinforel->datalen;
3092 inforelbuf = pbuf = nasm_malloc(inforellen);
3093 saa_rnbytes(pinforel, pbuf, saalen);
3094 saa_free(pinforel);
3096 /* build abbrev section */
3097 pabbrev = saa_init(1L);
3098 saa_write8(pabbrev,1); /* entry number LEB128u */
3099 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
3100 saa_write8(pabbrev,1); /* has children */
3101 /* the following attributes and forms are all LEB128u values */
3102 saa_write8(pabbrev,DW_AT_low_pc);
3103 saa_write8(pabbrev,DW_FORM_addr);
3104 saa_write8(pabbrev,DW_AT_high_pc);
3105 saa_write8(pabbrev,DW_FORM_addr);
3106 saa_write8(pabbrev,DW_AT_stmt_list);
3107 saa_write8(pabbrev,DW_FORM_data4);
3108 saa_write8(pabbrev,DW_AT_name);
3109 saa_write8(pabbrev,DW_FORM_string);
3110 saa_write8(pabbrev,DW_AT_producer);
3111 saa_write8(pabbrev,DW_FORM_string);
3112 saa_write8(pabbrev,DW_AT_language);
3113 saa_write8(pabbrev,DW_FORM_data2);
3114 saa_write16(pabbrev,0); /* end of entry */
3115 /* LEB128u usage same as above */
3116 saa_write8(pabbrev,2); /* entry number */
3117 saa_write8(pabbrev,DW_TAG_subprogram);
3118 saa_write8(pabbrev,0); /* no children */
3119 saa_write8(pabbrev,DW_AT_low_pc);
3120 saa_write8(pabbrev,DW_FORM_addr);
3121 saa_write8(pabbrev,DW_AT_frame_base);
3122 saa_write8(pabbrev,DW_FORM_data4);
3123 saa_write16(pabbrev,0); /* end of entry */
3124 abbrevlen = saalen = pabbrev->datalen;
3125 abbrevbuf = pbuf = nasm_malloc(saalen);
3126 saa_rnbytes(pabbrev, pbuf, saalen);
3127 saa_free(pabbrev);
3129 /* build line section */
3130 /* prolog */
3131 plines = saa_init(1L);
3132 saa_write8(plines,1); /* Minimum Instruction Length */
3133 saa_write8(plines,1); /* Initial value of 'is_stmt' */
3134 saa_write8(plines,line_base); /* Line Base */
3135 saa_write8(plines,line_range); /* Line Range */
3136 saa_write8(plines,opcode_base); /* Opcode Base */
3137 /* standard opcode lengths (# of LEB128u operands) */
3138 saa_write8(plines,0); /* Std opcode 1 length */
3139 saa_write8(plines,1); /* Std opcode 2 length */
3140 saa_write8(plines,1); /* Std opcode 3 length */
3141 saa_write8(plines,1); /* Std opcode 4 length */
3142 saa_write8(plines,1); /* Std opcode 5 length */
3143 saa_write8(plines,0); /* Std opcode 6 length */
3144 saa_write8(plines,0); /* Std opcode 7 length */
3145 saa_write8(plines,0); /* Std opcode 8 length */
3146 saa_write8(plines,1); /* Std opcode 9 length */
3147 saa_write8(plines,0); /* Std opcode 10 length */
3148 saa_write8(plines,0); /* Std opcode 11 length */
3149 saa_write8(plines,1); /* Std opcode 12 length */
3150 /* Directory Table */
3151 saa_write8(plines,0); /* End of table */
3152 /* File Name Table */
3153 ftentry = dwarf_flist;
3154 for (indx = 0; indx < dwarf_numfiles; indx++) {
3155 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
3156 saa_write8(plines,0); /* directory LEB128u */
3157 saa_write8(plines,0); /* time LEB128u */
3158 saa_write8(plines,0); /* size LEB128u */
3159 ftentry = ftentry->next;
3161 saa_write8(plines,0); /* End of table */
3162 linepoff = plines->datalen;
3163 linelen = linepoff + totlen + 10;
3164 linebuf = pbuf = nasm_malloc(linelen);
3165 WRITELONG(pbuf,linelen-4); /* initial length */
3166 WRITESHORT(pbuf,3); /* dwarf version */
3167 WRITELONG(pbuf,linepoff); /* offset to line number program */
3168 /* write line header */
3169 saalen = linepoff;
3170 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
3171 pbuf += linepoff;
3172 saa_free(plines);
3173 /* concatonate line program ranges */
3174 linepoff += 13;
3175 plinesrel = saa_init(1L);
3176 psect = dwarf_fsect;
3177 if (is_elf32()) {
3178 for (indx = 0; indx < dwarf_nsections; indx++) {
3179 saa_write32(plinesrel, linepoff);
3180 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
3181 saa_write32(plinesrel, (uint32_t) 0);
3182 plinep = psect->psaa;
3183 saalen = plinep->datalen;
3184 saa_rnbytes(plinep, pbuf, saalen);
3185 pbuf += saalen;
3186 linepoff += saalen;
3187 saa_free(plinep);
3188 /* done with this entry */
3189 psect = psect->next;
3191 } else if (is_elfx32()) {
3192 for (indx = 0; indx < dwarf_nsections; indx++) {
3193 saa_write32(plinesrel, linepoff);
3194 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
3195 saa_write32(plinesrel, 0);
3196 plinep = psect->psaa;
3197 saalen = plinep->datalen;
3198 saa_rnbytes(plinep, pbuf, saalen);
3199 pbuf += saalen;
3200 linepoff += saalen;
3201 saa_free(plinep);
3202 /* done with this entry */
3203 psect = psect->next;
3205 } else {
3206 nasm_assert(is_elf64());
3207 for (indx = 0; indx < dwarf_nsections; indx++) {
3208 saa_write64(plinesrel, linepoff);
3209 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
3210 saa_write64(plinesrel, (uint64_t) 0);
3211 plinep = psect->psaa;
3212 saalen = plinep->datalen;
3213 saa_rnbytes(plinep, pbuf, saalen);
3214 pbuf += saalen;
3215 linepoff += saalen;
3216 saa_free(plinep);
3217 /* done with this entry */
3218 psect = psect->next;
3222 /* build rela.lines section */
3223 linerellen =saalen = plinesrel->datalen;
3224 linerelbuf = pbuf = nasm_malloc(linerellen);
3225 saa_rnbytes(plinesrel, pbuf, saalen);
3226 saa_free(plinesrel);
3228 /* build frame section */
3229 framelen = 4;
3230 framebuf = pbuf = nasm_malloc(framelen);
3231 WRITELONG(pbuf,framelen-4); /* initial length */
3233 /* build loc section */
3234 loclen = 16;
3235 locbuf = pbuf = nasm_malloc(loclen);
3236 if (is_elf32()) {
3237 WRITELONG(pbuf,0); /* null beginning offset */
3238 WRITELONG(pbuf,0); /* null ending offset */
3239 } else {
3240 nasm_assert(is_elf64());
3241 WRITEDLONG(pbuf,0); /* null beginning offset */
3242 WRITEDLONG(pbuf,0); /* null ending offset */
3246 static void dwarf_cleanup(void)
3248 nasm_free(arangesbuf);
3249 nasm_free(arangesrelbuf);
3250 nasm_free(pubnamesbuf);
3251 nasm_free(infobuf);
3252 nasm_free(inforelbuf);
3253 nasm_free(abbrevbuf);
3254 nasm_free(linebuf);
3255 nasm_free(linerelbuf);
3256 nasm_free(framebuf);
3257 nasm_free(locbuf);
3260 static void dwarf_findfile(const char * fname)
3262 int finx;
3263 struct linelist *match;
3265 /* return if fname is current file name */
3266 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
3267 return;
3269 /* search for match */
3270 match = 0;
3271 if (dwarf_flist) {
3272 match = dwarf_flist;
3273 for (finx = 0; finx < dwarf_numfiles; finx++) {
3274 if (!(strcmp(fname, match->filename))) {
3275 dwarf_clist = match;
3276 return;
3281 /* add file name to end of list */
3282 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
3283 dwarf_numfiles++;
3284 dwarf_clist->line = dwarf_numfiles;
3285 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
3286 strcpy(dwarf_clist->filename,fname);
3287 dwarf_clist->next = 0;
3288 if (!dwarf_flist) { /* if first entry */
3289 dwarf_flist = dwarf_elist = dwarf_clist;
3290 dwarf_clist->last = 0;
3291 } else { /* chain to previous entry */
3292 dwarf_elist->next = dwarf_clist;
3293 dwarf_elist = dwarf_clist;
3297 static void dwarf_findsect(const int index)
3299 int sinx;
3300 struct sectlist *match;
3301 struct SAA *plinep;
3303 /* return if index is current section index */
3304 if (dwarf_csect && (dwarf_csect->section == index))
3305 return;
3307 /* search for match */
3308 match = 0;
3309 if (dwarf_fsect) {
3310 match = dwarf_fsect;
3311 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
3312 if (match->section == index) {
3313 dwarf_csect = match;
3314 return;
3316 match = match->next;
3320 /* add entry to end of list */
3321 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
3322 dwarf_nsections++;
3323 dwarf_csect->psaa = plinep = saa_init(1L);
3324 dwarf_csect->line = 1;
3325 dwarf_csect->offset = 0;
3326 dwarf_csect->file = 1;
3327 dwarf_csect->section = index;
3328 dwarf_csect->next = 0;
3329 /* set relocatable address at start of line program */
3330 saa_write8(plinep,DW_LNS_extended_op);
3331 saa_write8(plinep,is_elf64() ? 9 : 5); /* operand length */
3332 saa_write8(plinep,DW_LNE_set_address);
3333 if (is_elf64())
3334 saa_write64(plinep,0); /* Start Address */
3335 else
3336 saa_write32(plinep,0); /* Start Address */
3338 if (!dwarf_fsect) { /* if first entry */
3339 dwarf_fsect = dwarf_esect = dwarf_csect;
3340 dwarf_csect->last = 0;
3341 } else { /* chain to previous entry */
3342 dwarf_esect->next = dwarf_csect;
3343 dwarf_esect = dwarf_csect;
3347 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */