nasmlib/file.c: Windows _chsize_s() *returns* errno
[nasm.git] / output / outelf.c
blobfcae88be8b84a6797fb31764687a595c3a5d5bff
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 "output/outform.h"
50 #include "output/outlib.h"
51 #include "rbtree.h"
52 #include "ver.h"
54 #include "output/dwarf.h"
55 #include "output/stabs.h"
56 #include "output/outelf.h"
57 #include "output/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 const struct dfmt df_dwarf;
159 static const struct dfmt df_stabs;
160 static struct elf_symbol *lastsym;
162 /* common debugging routines */
163 static void debug_typevalue(int32_t);
165 /* stabs debugging routines */
166 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t);
167 static void stabs_output(int, void *);
168 static void stabs_generate(void);
169 static void stabs_cleanup(void);
171 /* dwarf debugging routines */
172 static void dwarf_init(void);
173 static void dwarf_linenum(const char *filename, int32_t linenumber, int32_t);
174 static void dwarf_output(int, void *);
175 static void dwarf_generate(void);
176 static void dwarf_cleanup(void);
177 static void dwarf_findfile(const char *);
178 static void dwarf_findsect(const int);
180 static bool is_elf64(void);
181 static bool is_elf32(void);
182 static bool is_elfx32(void);
185 * Special NASM section numbers which are used to define ELF special
186 * symbols.
188 static int32_t elf_gotpc_sect, elf_gotoff_sect;
189 static int32_t elf_got_sect, elf_plt_sect;
190 static int32_t elf_sym_sect, elf_gottpoff_sect, elf_tlsie_sect;
192 uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
193 uint8_t elf_abiver = 0; /* Current ABI version */
195 const struct elf_known_section elf_known_sections[] = {
196 { ".text", SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, 16 },
197 { ".rodata", SHT_PROGBITS, SHF_ALLOC, 4 },
198 { ".lrodata", SHT_PROGBITS, SHF_ALLOC, 4 },
199 { ".data", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4 },
200 { ".ldata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4 },
201 { ".bss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4 },
202 { ".lbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4 },
203 { ".tdata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4 },
204 { ".tbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4 },
205 { ".comment", SHT_PROGBITS, 0, 1 },
206 { NULL, SHT_PROGBITS, SHF_ALLOC, 1 } /* default */
209 /* parse section attributes */
210 void elf_section_attrib(char *name, char *attr, int pass,
211 uint32_t *flags_and, uint32_t *flags_or,
212 uint64_t *align, int *type)
214 char *opt, *val, *next;
216 opt = nasm_skip_spaces(attr);
217 if (!opt || !*opt)
218 return;
220 while ((opt = nasm_opt_val(opt, &val, &next))) {
221 if (!nasm_stricmp(opt, "align")) {
222 if (!val) {
223 nasm_error(ERR_NONFATAL,
224 "section align without value specified");
225 } else {
226 *align = atoi(val);
227 if (*align == 0) {
228 *align = SHA_ANY;
229 } else if (!is_power2(*align)) {
230 nasm_error(ERR_NONFATAL,
231 "section alignment %"PRId64" is not a power of two",
232 *align);
233 *align = SHA_ANY;
236 } else if (!nasm_stricmp(opt, "alloc")) {
237 *flags_and |= SHF_ALLOC;
238 *flags_or |= SHF_ALLOC;
239 } else if (!nasm_stricmp(opt, "noalloc")) {
240 *flags_and |= SHF_ALLOC;
241 *flags_or &= ~SHF_ALLOC;
242 } else if (!nasm_stricmp(opt, "exec")) {
243 *flags_and |= SHF_EXECINSTR;
244 *flags_or |= SHF_EXECINSTR;
245 } else if (!nasm_stricmp(opt, "noexec")) {
246 *flags_and |= SHF_EXECINSTR;
247 *flags_or &= ~SHF_EXECINSTR;
248 } else if (!nasm_stricmp(opt, "write")) {
249 *flags_and |= SHF_WRITE;
250 *flags_or |= SHF_WRITE;
251 } else if (!nasm_stricmp(opt, "tls")) {
252 *flags_and |= SHF_TLS;
253 *flags_or |= SHF_TLS;
254 } else if (!nasm_stricmp(opt, "nowrite")) {
255 *flags_and |= SHF_WRITE;
256 *flags_or &= ~SHF_WRITE;
257 } else if (!nasm_stricmp(opt, "progbits")) {
258 *type = SHT_PROGBITS;
259 } else if (!nasm_stricmp(opt, "nobits")) {
260 *type = SHT_NOBITS;
261 } else if (pass == 1) {
262 nasm_error(ERR_WARNING,
263 "Unknown section attribute '%s' ignored on"
264 " declaration of section `%s'", opt, name);
266 opt = next;
270 int elf_directive(enum directives directive, char *value, int pass)
272 int64_t n;
273 bool err;
274 char *p;
276 switch (directive) {
277 case D_OSABI:
278 if (pass == 2)
279 return 1; /* ignore in pass 2 */
281 n = readnum(value, &err);
282 if (err) {
283 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
284 return 1;
287 if (n < 0 || n > 255) {
288 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
289 return 1;
292 elf_osabi = n;
293 elf_abiver = 0;
295 p = strchr(value,',');
296 if (!p)
297 return 1;
299 n = readnum(p + 1, &err);
300 if (err || n < 0 || n > 255) {
301 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
302 return 1;
305 elf_abiver = n;
306 return 1;
308 default:
309 return 0;
313 static void elf_init(void)
315 sects = NULL;
316 nsects = sectlen = 0;
317 syms = saa_init((int32_t)sizeof(struct elf_symbol));
318 nlocals = nglobs = ndebugs = 0;
319 bsym = raa_init();
320 strs = saa_init(1L);
321 saa_wbytes(strs, "\0", 1L);
322 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
323 strslen = 2 + strlen(elf_module);
324 shstrtab = NULL;
325 shstrtablen = shstrtabsize = 0;;
326 add_sectname("", "");
328 fwds = NULL;
331 * FIXME: tlsie is Elf32 only and
332 * gottpoff is Elfx32|64 only.
335 elf_gotpc_sect = seg_alloc();
336 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
337 elf_gotoff_sect = seg_alloc();
338 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
339 elf_got_sect = seg_alloc();
340 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
341 elf_plt_sect = seg_alloc();
342 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
343 elf_sym_sect = seg_alloc();
344 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
345 elf_gottpoff_sect = seg_alloc();
346 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
347 elf_tlsie_sect = seg_alloc();
348 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
350 def_seg = seg_alloc();
353 static void elf_cleanup(void)
355 struct elf_reloc *r;
356 int i;
358 elf_write();
359 for (i = 0; i < nsects; i++) {
360 if (sects[i]->type != SHT_NOBITS)
361 saa_free(sects[i]->data);
362 if (sects[i]->head)
363 saa_free(sects[i]->rel);
364 while (sects[i]->head) {
365 r = sects[i]->head;
366 sects[i]->head = sects[i]->head->next;
367 nasm_free(r);
370 nasm_free(sects);
371 saa_free(syms);
372 raa_free(bsym);
373 saa_free(strs);
374 dfmt->cleanup();
377 /* add entry to the elf .shstrtab section */
378 static void add_sectname(char *firsthalf, char *secondhalf)
380 int len = strlen(firsthalf) + strlen(secondhalf);
381 while (shstrtablen + len + 1 > shstrtabsize)
382 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
383 strcpy(shstrtab + shstrtablen, firsthalf);
384 strcat(shstrtab + shstrtablen, secondhalf);
385 shstrtablen += len + 1;
388 static int elf_make_section(char *name, int type, int flags, int align)
390 struct elf_section *s;
392 s = nasm_zalloc(sizeof(*s));
394 if (type != SHT_NOBITS)
395 s->data = saa_init(1L);
396 s->tail = &s->head;
397 if (!strcmp(name, ".text"))
398 s->index = def_seg;
399 else
400 s->index = seg_alloc();
401 add_sectname("", name);
403 s->name = nasm_strdup(name);
404 s->type = type;
405 s->flags = flags;
406 s->align = align;
408 if (nsects >= sectlen)
409 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
410 sects[nsects++] = s;
412 return nsects - 1;
415 static int32_t elf_section_names(char *name, int pass, int *bits)
417 char *p;
418 uint32_t flags, flags_and, flags_or;
419 uint64_t align;
420 int type, i;
422 if (!name) {
423 *bits = ofmt->maxbits;
424 return def_seg;
427 p = nasm_skip_word(name);
428 if (*p)
429 *p++ = '\0';
430 flags_and = flags_or = type = align = 0;
432 elf_section_attrib(name, p, pass, &flags_and,
433 &flags_or, &align, &type);
435 if (!strcmp(name, ".shstrtab") ||
436 !strcmp(name, ".symtab") ||
437 !strcmp(name, ".strtab")) {
438 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
439 "name `%s'", name);
440 return NO_SEG;
443 for (i = 0; i < nsects; i++)
444 if (!strcmp(name, sects[i]->name))
445 break;
446 if (i == nsects) {
447 const struct elf_known_section *ks = elf_known_sections;
449 while (ks->name) {
450 if (!strcmp(name, ks->name))
451 break;
452 ks++;
455 type = type ? type : ks->type;
456 align = align ? align : ks->align;
457 flags = (ks->flags & ~flags_and) | flags_or;
459 i = elf_make_section(name, type, flags, align);
460 } else if (pass == 1) {
461 if ((type && sects[i]->type != type)
462 || (align && sects[i]->align != align)
463 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
464 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
465 " redeclaration of section `%s'", name);
468 return sects[i]->index;
471 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
472 int is_global, char *special)
474 int pos = strslen;
475 struct elf_symbol *sym;
476 bool special_used = false;
478 #if defined(DEBUG) && DEBUG>2
479 nasm_error(ERR_DEBUG,
480 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
481 name, segment, offset, is_global, special);
482 #endif
483 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
485 * This is a NASM special symbol. We never allow it into
486 * the ELF symbol table, even if it's a valid one. If it
487 * _isn't_ a valid one, we should barf immediately.
489 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
491 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
492 strcmp(name, "..got") && strcmp(name, "..plt") &&
493 strcmp(name, "..sym") && strcmp(name, "..gottpoff") &&
494 strcmp(name, "..tlsie"))
495 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
496 return;
499 if (is_global == 3) {
500 struct elf_symbol **s;
502 * Fix up a forward-reference symbol size from the first
503 * pass.
505 for (s = &fwds; *s; s = &(*s)->nextfwd)
506 if (!strcmp((*s)->name, name)) {
507 struct tokenval tokval;
508 expr *e;
509 char *p = nasm_skip_spaces(nasm_skip_word(special));
511 stdscan_reset();
512 stdscan_set(p);
513 tokval.t_type = TOKEN_INVALID;
514 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
515 if (e) {
516 if (!is_simple(e))
517 nasm_error(ERR_NONFATAL, "cannot use relocatable"
518 " expression as symbol size");
519 else
520 (*s)->size = reloc_value(e);
524 * Remove it from the list of unresolved sizes.
526 nasm_free((*s)->name);
527 *s = (*s)->nextfwd;
528 return;
530 return; /* it wasn't an important one */
533 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
534 strslen += 1 + strlen(name);
536 lastsym = sym = saa_wstruct(syms);
538 memset(&sym->symv, 0, sizeof(struct rbtree));
540 sym->strpos = pos;
541 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
542 sym->other = STV_DEFAULT;
543 sym->size = 0;
544 if (segment == NO_SEG)
545 sym->section = SHN_ABS;
546 else {
547 int i;
548 sym->section = SHN_UNDEF;
549 if (segment == def_seg) {
550 /* we have to be sure at least text section is there */
551 int tempint;
552 if (segment != elf_section_names(".text", 2, &tempint))
553 nasm_panic(0, "strange segment conditions in ELF driver");
555 for (i = 0; i < nsects; i++) {
556 if (segment == sects[i]->index) {
557 sym->section = i + 1;
558 break;
563 if (is_global == 2) {
564 sym->size = offset;
565 sym->symv.key = 0;
566 sym->section = SHN_COMMON;
568 * We have a common variable. Check the special text to see
569 * if it's a valid number and power of two; if so, store it
570 * as the alignment for the common variable.
572 if (special) {
573 bool err;
574 sym->symv.key = readnum(special, &err);
575 if (err)
576 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
577 " valid number", special);
578 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
579 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
580 " power of two", special);
582 special_used = true;
583 } else
584 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
586 if (sym->type == SYM_GLOBAL) {
588 * If sym->section == SHN_ABS, then the first line of the
589 * else section would cause a core dump, because its a reference
590 * beyond the end of the section array.
591 * This behaviour is exhibited by this code:
592 * GLOBAL crash_nasm
593 * crash_nasm equ 0
594 * To avoid such a crash, such requests are silently discarded.
595 * This may not be the best solution.
597 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
598 bsym = raa_write(bsym, segment, nglobs);
599 } else if (sym->section != SHN_ABS) {
601 * This is a global symbol; so we must add it to the rbtree
602 * of global symbols in its section.
604 * In addition, we check the special text for symbol
605 * type and size information.
607 sects[sym->section-1]->gsyms =
608 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
610 if (special) {
611 int n = strcspn(special, " \t");
613 if (!nasm_strnicmp(special, "function", n))
614 sym->type |= STT_FUNC;
615 else if (!nasm_strnicmp(special, "data", n) ||
616 !nasm_strnicmp(special, "object", n))
617 sym->type |= STT_OBJECT;
618 else if (!nasm_strnicmp(special, "notype", n))
619 sym->type |= STT_NOTYPE;
620 else
621 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
622 n, special);
623 special += n;
625 special = nasm_skip_spaces(special);
626 if (*special) {
627 n = strcspn(special, " \t");
628 if (!nasm_strnicmp(special, "default", n))
629 sym->other = STV_DEFAULT;
630 else if (!nasm_strnicmp(special, "internal", n))
631 sym->other = STV_INTERNAL;
632 else if (!nasm_strnicmp(special, "hidden", n))
633 sym->other = STV_HIDDEN;
634 else if (!nasm_strnicmp(special, "protected", n))
635 sym->other = STV_PROTECTED;
636 else
637 n = 0;
638 special += n;
641 if (*special) {
642 struct tokenval tokval;
643 expr *e;
644 int fwd = 0;
645 char *saveme = stdscan_get();
647 while (special[n] && nasm_isspace(special[n]))
648 n++;
650 * We have a size expression; attempt to
651 * evaluate it.
653 stdscan_reset();
654 stdscan_set(special + n);
655 tokval.t_type = TOKEN_INVALID;
656 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
657 if (fwd) {
658 sym->nextfwd = fwds;
659 fwds = sym;
660 sym->name = nasm_strdup(name);
661 } else if (e) {
662 if (!is_simple(e))
663 nasm_error(ERR_NONFATAL, "cannot use relocatable"
664 " expression as symbol size");
665 else
666 sym->size = reloc_value(e);
668 stdscan_set(saveme);
670 special_used = true;
673 * If TLS segment, mark symbol accordingly.
675 if (sects[sym->section - 1]->flags & SHF_TLS) {
676 sym->type &= 0xf0;
677 sym->type |= STT_TLS;
680 sym->globnum = nglobs;
681 nglobs++;
682 } else
683 nlocals++;
685 if (special && !special_used)
686 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
689 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
690 int64_t offset, int type)
692 struct elf_reloc *r;
694 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
695 sect->tail = &r->next;
697 r->address = sect->len;
698 r->offset = offset;
700 if (segment != NO_SEG) {
701 int i;
702 for (i = 0; i < nsects; i++)
703 if (segment == sects[i]->index)
704 r->symbol = i + 2;
705 if (!r->symbol)
706 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
708 r->type = type;
710 sect->nrelocs++;
714 * This routine deals with ..got and ..sym relocations: the more
715 * complicated kinds. In shared-library writing, some relocations
716 * with respect to global symbols must refer to the precise symbol
717 * rather than referring to an offset from the base of the section
718 * _containing_ the symbol. Such relocations call to this routine,
719 * which searches the symbol list for the symbol in question.
721 * R_386_GOT32 | R_X86_64_GOT32 references require the _exact_ symbol address to be
722 * used; R_386_32 | R_X86_64_32 references can be at an offset from the symbol.
723 * The boolean argument `exact' tells us this.
725 * Return value is the adjusted value of `addr', having become an
726 * offset from the symbol rather than the section. Should always be
727 * zero when returning from an exact call.
729 * Limitation: if you define two symbols at the same place,
730 * confusion will occur.
732 * Inefficiency: we search, currently, using a linked list which
733 * isn't even necessarily sorted.
735 static int64_t elf_add_gsym_reloc(struct elf_section *sect,
736 int32_t segment, uint64_t offset,
737 int64_t pcrel, int type, bool exact)
739 struct elf_reloc *r;
740 struct elf_section *s;
741 struct elf_symbol *sym;
742 struct rbtree *srb;
743 int i;
746 * First look up the segment/offset pair and find a global
747 * symbol corresponding to it. If it's not one of our segments,
748 * then it must be an external symbol, in which case we're fine
749 * doing a normal elf_add_reloc after first sanity-checking
750 * that the offset from the symbol is zero.
752 s = NULL;
753 for (i = 0; i < nsects; i++)
754 if (segment == sects[i]->index) {
755 s = sects[i];
756 break;
759 if (!s) {
760 if (exact && offset)
761 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
762 else
763 elf_add_reloc(sect, segment, offset - pcrel, type);
764 return 0;
767 srb = rb_search(s->gsyms, offset);
768 if (!srb || (exact && srb->key != offset)) {
769 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
770 " for this reference");
771 return 0;
773 sym = container_of(srb, struct elf_symbol, symv);
775 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
776 sect->tail = &r->next;
778 r->next = NULL;
779 r->address = sect->len;
780 r->offset = offset - pcrel - sym->symv.key;
781 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
782 r->type = type;
784 sect->nrelocs++;
785 return r->offset;
788 static void elf32_out(int32_t segto, const void *data,
789 enum out_type type, uint64_t size,
790 int32_t segment, int32_t wrt)
792 struct elf_section *s;
793 int64_t addr;
794 int reltype, bytes;
795 int i;
796 static struct symlininfo sinfo;
799 * handle absolute-assembly (structure definitions)
801 if (segto == NO_SEG) {
802 if (type != OUT_RESERVE)
803 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
804 " space");
805 return;
808 s = NULL;
809 for (i = 0; i < nsects; i++)
810 if (segto == sects[i]->index) {
811 s = sects[i];
812 break;
814 if (!s) {
815 int tempint; /* ignored */
816 if (segto != elf_section_names(".text", 2, &tempint))
817 nasm_panic(0, "strange segment conditions in ELF driver");
818 else {
819 s = sects[nsects - 1];
820 i = nsects - 1;
824 /* again some stabs debugging stuff */
825 sinfo.offset = s->len;
826 sinfo.section = i;
827 sinfo.segto = segto;
828 sinfo.name = s->name;
829 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
830 /* end of debugging stuff */
832 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
833 nasm_error(ERR_WARNING, "attempt to initialize memory in"
834 " BSS section `%s': ignored", s->name);
835 s->len += realsize(type, size);
836 return;
839 switch (type) {
840 case OUT_RESERVE:
841 if (s->type == SHT_PROGBITS) {
842 nasm_error(ERR_WARNING, "uninitialized space declared in"
843 " non-BSS section `%s': zeroing", s->name);
844 elf_sect_write(s, NULL, size);
845 } else
846 s->len += size;
847 break;
849 case OUT_RAWDATA:
850 if (segment != NO_SEG)
851 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
852 elf_sect_write(s, data, size);
853 break;
855 case OUT_ADDRESS:
857 bool gnu16 = false;
858 int asize = abs((int)size);
860 addr = *(int64_t *)data;
861 if (segment != NO_SEG) {
862 if (segment % 2) {
863 nasm_error(ERR_NONFATAL, "ELF format does not support"
864 " segment base references");
865 } else {
866 if (wrt == NO_SEG) {
868 * The if() is a hack to deal with compilers which
869 * don't handle switch() statements with 64-bit
870 * expressions.
872 switch (asize) {
873 case 1:
874 gnu16 = true;
875 elf_add_reloc(s, segment, 0, R_386_8);
876 break;
877 case 2:
878 gnu16 = true;
879 elf_add_reloc(s, segment, 0, R_386_16);
880 break;
881 case 4:
882 elf_add_reloc(s, segment, 0, R_386_32);
883 break;
884 default: /* Error issued further down */
885 break;
887 } else if (wrt == elf_gotpc_sect + 1) {
889 * The user will supply GOT relative to $$. ELF
890 * will let us have GOT relative to $. So we
891 * need to fix up the data item by $-$$.
893 addr += s->len;
894 elf_add_reloc(s, segment, 0, R_386_GOTPC);
895 } else if (wrt == elf_gotoff_sect + 1) {
896 elf_add_reloc(s, segment, 0, R_386_GOTOFF);
897 } else if (wrt == elf_tlsie_sect + 1) {
898 addr = elf_add_gsym_reloc(s, segment, addr, 0,
899 R_386_TLS_IE, true);
900 } else if (wrt == elf_got_sect + 1) {
901 addr = elf_add_gsym_reloc(s, segment, addr, 0,
902 R_386_GOT32, true);
903 } else if (wrt == elf_sym_sect + 1) {
904 switch (asize) {
905 case 1:
906 gnu16 = true;
907 addr = elf_add_gsym_reloc(s, segment, addr, 0,
908 R_386_8, false);
909 break;
910 case 2:
911 gnu16 = true;
912 addr = elf_add_gsym_reloc(s, segment, addr, 0,
913 R_386_16, false);
914 break;
915 case 4:
916 addr = elf_add_gsym_reloc(s, segment, addr, 0,
917 R_386_32, false);
918 break;
919 default:
920 break;
922 } else if (wrt == elf_plt_sect + 1) {
923 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
924 "relative PLT references");
925 } else {
926 nasm_error(ERR_NONFATAL, "ELF format does not support this"
927 " use of WRT");
928 wrt = NO_SEG; /* we can at least _try_ to continue */
933 if (gnu16) {
934 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
935 "8- or 16-bit relocations in ELF32 is a GNU extension");
936 } else if (asize != 4 && segment != NO_SEG) {
937 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
939 elf_sect_writeaddr(s, addr, asize);
940 break;
943 case OUT_REL1ADR:
944 reltype = R_386_PC8;
945 bytes = 1;
946 goto rel12adr;
947 case OUT_REL2ADR:
948 reltype = R_386_PC16;
949 bytes = 2;
950 goto rel12adr;
952 rel12adr:
953 addr = *(int64_t *)data - size;
954 nasm_assert(segment != segto);
955 if (segment != NO_SEG && segment % 2) {
956 nasm_error(ERR_NONFATAL, "ELF format does not support"
957 " segment base references");
958 } else {
959 if (wrt == NO_SEG) {
960 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
961 "8- or 16-bit relocations in ELF is a GNU extension");
962 elf_add_reloc(s, segment, 0, reltype);
963 } else {
964 nasm_error(ERR_NONFATAL,
965 "Unsupported non-32-bit ELF relocation");
968 elf_sect_writeaddr(s, addr, bytes);
969 break;
971 case OUT_REL4ADR:
972 addr = *(int64_t *)data - size;
973 if (segment == segto)
974 nasm_panic(0, "intra-segment OUT_REL4ADR");
975 if (segment != NO_SEG && segment % 2) {
976 nasm_error(ERR_NONFATAL, "ELF format does not support"
977 " segment base references");
978 } else {
979 if (wrt == NO_SEG) {
980 elf_add_reloc(s, segment, 0, R_386_PC32);
981 } else if (wrt == elf_plt_sect + 1) {
982 elf_add_reloc(s, segment, 0, R_386_PLT32);
983 } else if (wrt == elf_gotpc_sect + 1 ||
984 wrt == elf_gotoff_sect + 1 ||
985 wrt == elf_got_sect + 1) {
986 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
987 "relative GOT references");
988 } else {
989 nasm_error(ERR_NONFATAL, "ELF format does not support this"
990 " use of WRT");
991 wrt = NO_SEG; /* we can at least _try_ to continue */
994 elf_sect_writeaddr(s, addr, 4);
995 break;
997 case OUT_REL8ADR:
998 nasm_error(ERR_NONFATAL, "32-bit ELF format does not support 64-bit relocations");
999 addr = 0;
1000 elf_sect_writeaddr(s, addr, 8);
1001 break;
1004 static void elf64_out(int32_t segto, const void *data,
1005 enum out_type type, uint64_t size,
1006 int32_t segment, int32_t wrt)
1008 struct elf_section *s;
1009 int64_t addr;
1010 int reltype, bytes;
1011 int i;
1012 static struct symlininfo sinfo;
1015 * handle absolute-assembly (structure definitions)
1017 if (segto == NO_SEG) {
1018 if (type != OUT_RESERVE)
1019 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1020 " space");
1021 return;
1024 s = NULL;
1025 for (i = 0; i < nsects; i++)
1026 if (segto == sects[i]->index) {
1027 s = sects[i];
1028 break;
1030 if (!s) {
1031 int tempint; /* ignored */
1032 if (segto != elf_section_names(".text", 2, &tempint))
1033 nasm_panic(0, "strange segment conditions in ELF driver");
1034 else {
1035 s = sects[nsects - 1];
1036 i = nsects - 1;
1040 /* again some stabs debugging stuff */
1041 sinfo.offset = s->len;
1042 sinfo.section = i;
1043 sinfo.segto = segto;
1044 sinfo.name = s->name;
1045 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1046 /* end of debugging stuff */
1048 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1049 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1050 " BSS section `%s': ignored", s->name);
1051 s->len += realsize(type, size);
1052 return;
1055 switch (type) {
1056 case OUT_RESERVE:
1057 if (s->type == SHT_PROGBITS) {
1058 nasm_error(ERR_WARNING, "uninitialized space declared in"
1059 " non-BSS section `%s': zeroing", s->name);
1060 elf_sect_write(s, NULL, size);
1061 } else
1062 s->len += size;
1063 break;
1065 case OUT_RAWDATA:
1066 if (segment != NO_SEG)
1067 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1068 elf_sect_write(s, data, size);
1069 break;
1071 case OUT_ADDRESS:
1073 int isize = (int)size;
1074 int asize = abs((int)size);
1076 addr = *(int64_t *)data;
1077 if (segment == NO_SEG) {
1078 /* Do nothing */
1079 } else if (segment % 2) {
1080 nasm_error(ERR_NONFATAL, "ELF format does not support"
1081 " segment base references");
1082 } else {
1083 if (wrt == NO_SEG) {
1084 switch (isize) {
1085 case 1:
1086 case -1:
1087 elf_add_reloc(s, segment, addr, R_X86_64_8);
1088 break;
1089 case 2:
1090 case -2:
1091 elf_add_reloc(s, segment, addr, R_X86_64_16);
1092 break;
1093 case 4:
1094 elf_add_reloc(s, segment, addr, R_X86_64_32);
1095 break;
1096 case -4:
1097 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1098 break;
1099 case 8:
1100 case -8:
1101 elf_add_reloc(s, segment, addr, R_X86_64_64);
1102 break;
1103 default:
1104 nasm_panic(0, "internal error elf64-hpa-871");
1105 break;
1107 addr = 0;
1108 } else if (wrt == elf_gotpc_sect + 1) {
1110 * The user will supply GOT relative to $$. ELF
1111 * will let us have GOT relative to $. So we
1112 * need to fix up the data item by $-$$.
1114 addr += s->len;
1115 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1116 addr = 0;
1117 } else if (wrt == elf_gotoff_sect + 1) {
1118 if (asize != 8) {
1119 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
1120 "references to be qword");
1121 } else {
1122 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
1123 addr = 0;
1125 } else if (wrt == elf_got_sect + 1) {
1126 switch (asize) {
1127 case 4:
1128 elf_add_gsym_reloc(s, segment, addr, 0,
1129 R_X86_64_GOT32, true);
1130 addr = 0;
1131 break;
1132 case 8:
1133 elf_add_gsym_reloc(s, segment, addr, 0,
1134 R_X86_64_GOT64, true);
1135 addr = 0;
1136 break;
1137 default:
1138 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1139 break;
1141 } else if (wrt == elf_sym_sect + 1) {
1142 switch (isize) {
1143 case 1:
1144 case -1:
1145 elf_add_gsym_reloc(s, segment, addr, 0,
1146 R_X86_64_8, false);
1147 addr = 0;
1148 break;
1149 case 2:
1150 case -2:
1151 elf_add_gsym_reloc(s, segment, addr, 0,
1152 R_X86_64_16, false);
1153 addr = 0;
1154 break;
1155 case 4:
1156 elf_add_gsym_reloc(s, segment, addr, 0,
1157 R_X86_64_32, false);
1158 addr = 0;
1159 break;
1160 case -4:
1161 elf_add_gsym_reloc(s, segment, addr, 0,
1162 R_X86_64_32S, false);
1163 addr = 0;
1164 break;
1165 case 8:
1166 case -8:
1167 elf_add_gsym_reloc(s, segment, addr, 0,
1168 R_X86_64_64, false);
1169 addr = 0;
1170 break;
1171 default:
1172 nasm_panic(0, "internal error elf64-hpa-903");
1173 break;
1175 } else if (wrt == elf_plt_sect + 1) {
1176 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1177 "relative PLT references");
1178 } else {
1179 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1180 " use of WRT");
1183 elf_sect_writeaddr(s, addr, asize);
1184 break;
1187 case OUT_REL1ADR:
1188 reltype = R_X86_64_PC8;
1189 bytes = 1;
1190 goto rel12adr;
1192 case OUT_REL2ADR:
1193 reltype = R_X86_64_PC16;
1194 bytes = 2;
1195 goto rel12adr;
1197 rel12adr:
1198 addr = *(int64_t *)data - size;
1199 if (segment == segto)
1200 nasm_panic(0, "intra-segment OUT_REL1ADR");
1201 if (segment == NO_SEG) {
1202 /* Do nothing */
1203 } else if (segment % 2) {
1204 nasm_error(ERR_NONFATAL, "ELF format does not support"
1205 " segment base references");
1206 } else {
1207 if (wrt == NO_SEG) {
1208 elf_add_reloc(s, segment, addr, reltype);
1209 addr = 0;
1210 } else {
1211 nasm_error(ERR_NONFATAL,
1212 "Unsupported non-32-bit ELF relocation");
1215 elf_sect_writeaddr(s, addr, bytes);
1216 break;
1218 case OUT_REL4ADR:
1219 addr = *(int64_t *)data - size;
1220 if (segment == segto)
1221 nasm_panic(0, "intra-segment OUT_REL4ADR");
1222 if (segment == NO_SEG) {
1223 /* Do nothing */
1224 } else if (segment % 2) {
1225 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1226 " segment base references");
1227 } else {
1228 if (wrt == NO_SEG) {
1229 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1230 addr = 0;
1231 } else if (wrt == elf_plt_sect + 1) {
1232 elf_add_gsym_reloc(s, segment, addr+size, size,
1233 R_X86_64_PLT32, true);
1234 addr = 0;
1235 } else if (wrt == elf_gotpc_sect + 1 ||
1236 wrt == elf_got_sect + 1) {
1237 elf_add_gsym_reloc(s, segment, addr+size, size,
1238 R_X86_64_GOTPCREL, true);
1239 addr = 0;
1240 } else if (wrt == elf_gotoff_sect + 1 ||
1241 wrt == elf_got_sect + 1) {
1242 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1243 "qword absolute");
1244 } else if (wrt == elf_gottpoff_sect + 1) {
1245 elf_add_gsym_reloc(s, segment, addr+size, size,
1246 R_X86_64_GOTTPOFF, true);
1247 addr = 0;
1248 } else {
1249 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1250 " use of WRT");
1253 elf_sect_writeaddr(s, addr, 4);
1254 break;
1256 case OUT_REL8ADR:
1257 addr = *(int64_t *)data - size;
1258 if (segment == segto)
1259 nasm_panic(0, "intra-segment OUT_REL8ADR");
1260 if (segment == NO_SEG) {
1261 /* Do nothing */
1262 } else if (segment % 2) {
1263 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1264 " segment base references");
1265 } else {
1266 if (wrt == NO_SEG) {
1267 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1268 addr = 0;
1269 } else if (wrt == elf_gotpc_sect + 1 ||
1270 wrt == elf_got_sect + 1) {
1271 elf_add_gsym_reloc(s, segment, addr+size, size,
1272 R_X86_64_GOTPCREL64, true);
1273 addr = 0;
1274 } else if (wrt == elf_gotoff_sect + 1 ||
1275 wrt == elf_got_sect + 1) {
1276 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1277 "absolute");
1278 } else if (wrt == elf_gottpoff_sect + 1) {
1279 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1280 "dword");
1281 } else {
1282 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1283 " use of WRT");
1286 elf_sect_writeaddr(s, addr, 8);
1287 break;
1291 static void elfx32_out(int32_t segto, const void *data,
1292 enum out_type type, uint64_t size,
1293 int32_t segment, int32_t wrt)
1295 struct elf_section *s;
1296 int64_t addr;
1297 int reltype, bytes;
1298 int i;
1299 static struct symlininfo sinfo;
1302 * handle absolute-assembly (structure definitions)
1304 if (segto == NO_SEG) {
1305 if (type != OUT_RESERVE)
1306 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1307 " space");
1308 return;
1311 s = NULL;
1312 for (i = 0; i < nsects; i++)
1313 if (segto == sects[i]->index) {
1314 s = sects[i];
1315 break;
1317 if (!s) {
1318 int tempint; /* ignored */
1319 if (segto != elf_section_names(".text", 2, &tempint))
1320 nasm_panic(0, "strange segment conditions in ELF driver");
1321 else {
1322 s = sects[nsects - 1];
1323 i = nsects - 1;
1327 /* again some stabs debugging stuff */
1328 sinfo.offset = s->len;
1329 sinfo.section = i;
1330 sinfo.segto = segto;
1331 sinfo.name = s->name;
1332 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1333 /* end of debugging stuff */
1335 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1336 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1337 " BSS section `%s': ignored", s->name);
1338 s->len += realsize(type, size);
1339 return;
1342 switch (type) {
1343 case OUT_RESERVE:
1344 if (s->type == SHT_PROGBITS) {
1345 nasm_error(ERR_WARNING, "uninitialized space declared in"
1346 " non-BSS section `%s': zeroing", s->name);
1347 elf_sect_write(s, NULL, size);
1348 } else
1349 s->len += size;
1350 break;
1352 case OUT_RAWDATA:
1353 if (segment != NO_SEG)
1354 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1355 elf_sect_write(s, data, size);
1356 break;
1358 case OUT_ADDRESS:
1360 int isize = (int)size;
1361 int asize = abs((int)size);
1363 addr = *(int64_t *)data;
1364 if (segment == NO_SEG) {
1365 /* Do nothing */
1366 } else if (segment % 2) {
1367 nasm_error(ERR_NONFATAL, "ELF format does not support"
1368 " segment base references");
1369 } else {
1370 if (wrt == NO_SEG) {
1371 switch (isize) {
1372 case 1:
1373 case -1:
1374 elf_add_reloc(s, segment, addr, R_X86_64_8);
1375 break;
1376 case 2:
1377 case -2:
1378 elf_add_reloc(s, segment, addr, R_X86_64_16);
1379 break;
1380 case 4:
1381 elf_add_reloc(s, segment, addr, R_X86_64_32);
1382 break;
1383 case -4:
1384 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1385 break;
1386 case 8:
1387 case -8:
1388 elf_add_reloc(s, segment, addr, R_X86_64_64);
1389 break;
1390 default:
1391 nasm_panic(0, "internal error elfx32-hpa-871");
1392 break;
1394 addr = 0;
1395 } else if (wrt == elf_gotpc_sect + 1) {
1397 * The user will supply GOT relative to $$. ELF
1398 * will let us have GOT relative to $. So we
1399 * need to fix up the data item by $-$$.
1401 addr += s->len;
1402 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1403 addr = 0;
1404 } else if (wrt == elf_gotoff_sect + 1) {
1405 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
1406 "R_X86_64_GOTOFF64");
1407 } else if (wrt == elf_got_sect + 1) {
1408 switch (asize) {
1409 case 4:
1410 elf_add_gsym_reloc(s, segment, addr, 0,
1411 R_X86_64_GOT32, true);
1412 addr = 0;
1413 break;
1414 default:
1415 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1416 break;
1418 } else if (wrt == elf_sym_sect + 1) {
1419 switch (isize) {
1420 case 1:
1421 case -1:
1422 elf_add_gsym_reloc(s, segment, addr, 0,
1423 R_X86_64_8, false);
1424 addr = 0;
1425 break;
1426 case 2:
1427 case -2:
1428 elf_add_gsym_reloc(s, segment, addr, 0,
1429 R_X86_64_16, false);
1430 addr = 0;
1431 break;
1432 case 4:
1433 elf_add_gsym_reloc(s, segment, addr, 0,
1434 R_X86_64_32, false);
1435 addr = 0;
1436 break;
1437 case -4:
1438 elf_add_gsym_reloc(s, segment, addr, 0,
1439 R_X86_64_32S, false);
1440 addr = 0;
1441 break;
1442 case 8:
1443 case -8:
1444 elf_add_gsym_reloc(s, segment, addr, 0,
1445 R_X86_64_64, false);
1446 addr = 0;
1447 break;
1448 default:
1449 nasm_panic(0, "internal error elfx32-hpa-903");
1450 break;
1452 } else if (wrt == elf_plt_sect + 1) {
1453 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1454 "relative PLT references");
1455 } else {
1456 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1457 " use of WRT");
1460 elf_sect_writeaddr(s, addr, asize);
1461 break;
1464 case OUT_REL1ADR:
1465 reltype = R_X86_64_PC8;
1466 bytes = 1;
1467 goto rel12adr;
1469 case OUT_REL2ADR:
1470 reltype = R_X86_64_PC16;
1471 bytes = 2;
1472 goto rel12adr;
1474 rel12adr:
1475 addr = *(int64_t *)data - size;
1476 if (segment == segto)
1477 nasm_panic(0, "intra-segment OUT_REL1ADR");
1478 if (segment == NO_SEG) {
1479 /* Do nothing */
1480 } else if (segment % 2) {
1481 nasm_error(ERR_NONFATAL, "ELF format does not support"
1482 " segment base references");
1483 } else {
1484 if (wrt == NO_SEG) {
1485 elf_add_reloc(s, segment, addr, reltype);
1486 addr = 0;
1487 } else {
1488 nasm_error(ERR_NONFATAL,
1489 "Unsupported non-32-bit ELF relocation");
1492 elf_sect_writeaddr(s, addr, bytes);
1493 break;
1495 case OUT_REL4ADR:
1496 addr = *(int64_t *)data - size;
1497 if (segment == segto)
1498 nasm_panic(0, "intra-segment OUT_REL4ADR");
1499 if (segment == NO_SEG) {
1500 /* Do nothing */
1501 } else if (segment % 2) {
1502 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
1503 " segment base references");
1504 } else {
1505 if (wrt == NO_SEG) {
1506 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1507 addr = 0;
1508 } else if (wrt == elf_plt_sect + 1) {
1509 elf_add_gsym_reloc(s, segment, addr+size, size,
1510 R_X86_64_PLT32, true);
1511 addr = 0;
1512 } else if (wrt == elf_gotpc_sect + 1 ||
1513 wrt == elf_got_sect + 1) {
1514 elf_add_gsym_reloc(s, segment, addr+size, size,
1515 R_X86_64_GOTPCREL, true);
1516 addr = 0;
1517 } else if (wrt == elf_gotoff_sect + 1 ||
1518 wrt == elf_got_sect + 1) {
1519 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
1520 } else if (wrt == elf_gottpoff_sect + 1) {
1521 elf_add_gsym_reloc(s, segment, addr+size, size,
1522 R_X86_64_GOTTPOFF, true);
1523 addr = 0;
1524 } else {
1525 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
1526 " use of WRT");
1529 elf_sect_writeaddr(s, addr, 4);
1530 break;
1532 case OUT_REL8ADR:
1533 nasm_error(ERR_NONFATAL, "32-bit ELF format does not support 64-bit relocations");
1534 addr = 0;
1535 elf_sect_writeaddr(s, addr, 8);
1536 break;
1540 static void elf_write(void)
1542 int align;
1543 char *p;
1544 int i;
1546 struct SAA *symtab;
1547 int32_t symtablen, symtablocal;
1550 * Work out how many sections we will have. We have SHN_UNDEF,
1551 * then the flexible user sections, then the fixed sections
1552 * `.shstrtab', `.symtab' and `.strtab', then optionally
1553 * relocation sections for the user sections.
1555 nsections = sec_numspecial + 1;
1556 if (dfmt == &df_stabs)
1557 nsections += 3;
1558 else if (dfmt == &df_dwarf)
1559 nsections += 10;
1561 add_sectname("", ".shstrtab");
1562 add_sectname("", ".symtab");
1563 add_sectname("", ".strtab");
1564 for (i = 0; i < nsects; i++) {
1565 nsections++; /* for the section itself */
1566 if (sects[i]->head) {
1567 nsections++; /* for its relocations */
1568 add_sectname(is_elf32() ? ".rel" : ".rela", sects[i]->name);
1572 if (dfmt == &df_stabs) {
1573 /* in case the debug information is wanted, just add these three sections... */
1574 add_sectname("", ".stab");
1575 add_sectname("", ".stabstr");
1576 add_sectname(".rel", ".stab");
1577 } else if (dfmt == &df_dwarf) {
1578 /* the dwarf debug standard specifies the following ten sections,
1579 not all of which are currently implemented,
1580 although all of them are defined. */
1581 #define debug_aranges (int64_t) (nsections-10)
1582 #define debug_info (int64_t) (nsections-7)
1583 #define debug_abbrev (int64_t) (nsections-5)
1584 #define debug_line (int64_t) (nsections-4)
1585 add_sectname("", ".debug_aranges");
1586 add_sectname(".rela", ".debug_aranges");
1587 add_sectname("", ".debug_pubnames");
1588 add_sectname("", ".debug_info");
1589 add_sectname(".rela", ".debug_info");
1590 add_sectname("", ".debug_abbrev");
1591 add_sectname("", ".debug_line");
1592 add_sectname(".rela", ".debug_line");
1593 add_sectname("", ".debug_frame");
1594 add_sectname("", ".debug_loc");
1598 * Output the ELF header.
1600 if (is_elf32() || is_elfx32()) {
1601 nasm_write("\177ELF\1\1\1", 7, ofile);
1602 fputc(elf_osabi, ofile);
1603 fputc(elf_abiver, ofile);
1604 fwritezero(7, ofile);
1605 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1606 fwriteint16_t(is_elf32() ? EM_386 : EM_X86_64, ofile); /* processor ID */
1607 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1608 fwriteint32_t(0L, ofile); /* no entry point */
1609 fwriteint32_t(0L, ofile); /* no program header table */
1610 fwriteint32_t(0x40L, ofile); /* section headers straight after ELF header plus alignment */
1611 fwriteint32_t(0L, ofile); /* no special flags */
1612 fwriteint16_t(0x34, ofile); /* size of ELF header */
1613 fwriteint16_t(0, ofile); /* no program header table, again */
1614 fwriteint16_t(0, ofile); /* still no program header table */
1615 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
1616 fwriteint16_t(nsections, ofile); /* number of sections */
1617 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for section header table */
1619 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
1620 fwriteint32_t(0L, ofile);
1621 fwriteint32_t(0L, ofile);
1622 } else {
1623 nasm_assert(is_elf64());
1624 nasm_write("\177ELF\2\1\1", 7, ofile);
1625 fputc(elf_osabi, ofile);
1626 fputc(elf_abiver, ofile);
1627 fwritezero(7, ofile);
1628 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1629 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1630 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1631 fwriteint64_t(0L, ofile); /* no entry point */
1632 fwriteint64_t(0L, ofile); /* no program header table */
1633 fwriteint64_t(0x40L, ofile); /* section headers straight after ELF header plus alignment */
1634 fwriteint32_t(0L, ofile); /* no special flags */
1635 fwriteint16_t(0x40, ofile); /* size of ELF header */
1636 fwriteint16_t(0, ofile); /* no program header table, again */
1637 fwriteint16_t(0, ofile); /* still no program header table */
1638 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1639 fwriteint16_t(nsections, ofile); /* number of sections */
1640 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for section header table */
1644 * Build the symbol table and relocation tables.
1646 symtab = elf_build_symtab(&symtablen, &symtablocal);
1647 for (i = 0; i < nsects; i++)
1648 if (sects[i]->head)
1649 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1650 sects[i]->head);
1653 * Now output the section header table.
1656 elf_foffs = 0x40 + (is_elf64() ? sizeof(Elf64_Shdr): sizeof(Elf32_Shdr)) * nsections;
1657 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1658 elf_foffs += align;
1659 elf_nsect = 0;
1660 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1662 /* SHN_UNDEF */
1663 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1664 p = shstrtab + 1;
1666 /* The normal sections */
1667 for (i = 0; i < nsects; i++) {
1668 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1669 (sects[i]->type == SHT_PROGBITS ?
1670 sects[i]->data : NULL), true,
1671 sects[i]->len, 0, 0, sects[i]->align, 0);
1672 p += strlen(p) + 1;
1675 /* .shstrtab */
1676 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1677 shstrtablen, 0, 0, 1, 0);
1678 p += strlen(p) + 1;
1680 /* .symtab */
1681 if (is_elf64())
1682 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1683 symtablen, sec_strtab, symtablocal, 8, 24);
1684 else
1685 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1686 symtablen, sec_strtab, symtablocal, 4, 16);
1687 p += strlen(p) + 1;
1689 /* .strtab */
1690 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1691 strslen, 0, 0, 1, 0);
1692 p += strlen(p) + 1;
1694 /* The relocation sections */
1695 if (is_elf32()) {
1696 for (i = 0; i < nsects; i++) {
1697 if (sects[i]->head) {
1698 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1699 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1700 p += strlen(p) + 1;
1703 } else if (is_elfx32()) {
1704 for (i = 0; i < nsects; i++) {
1705 if (sects[i]->head) {
1706 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1707 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1708 p += strlen(p) + 1;
1711 } else {
1712 nasm_assert(is_elf64());
1713 for (i = 0; i < nsects; i++) {
1714 if (sects[i]->head) {
1715 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1716 sects[i]->rellen, sec_symtab, i + 1, 8, 24);
1717 p += strlen(p) + 1;
1722 if (dfmt == &df_stabs) {
1723 /* for debugging information, create the last three sections
1724 which are the .stab , .stabstr and .rel.stab sections respectively */
1726 /* this function call creates the stab sections in memory */
1727 stabs_generate();
1729 if (stabbuf && stabstrbuf && stabrelbuf) {
1730 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1731 stablen, sec_stabstr, 0, 4, 12);
1732 p += strlen(p) + 1;
1734 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1735 stabstrlen, 0, 0, 4, 0);
1736 p += strlen(p) + 1;
1738 /* link -> symtable info -> section to refer to */
1739 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1740 stabrellen, sec_symtab, sec_stab, 4, is_elf64() ? 16 : 8);
1741 p += strlen(p) + 1;
1743 } else if (dfmt == &df_dwarf) {
1744 /* for dwarf debugging information, create the ten dwarf sections */
1746 /* this function call creates the dwarf sections in memory */
1747 if (dwarf_fsect)
1748 dwarf_generate();
1750 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1751 arangeslen, 0, 0, 1, 0);
1752 p += strlen(p) + 1;
1754 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1755 arangesrellen, sec_symtab,
1756 is_elf64() ? debug_aranges : sec_debug_aranges,
1757 1, is_elf64() ? 24 : 12);
1758 p += strlen(p) + 1;
1760 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1761 false, pubnameslen, 0, 0, 1, 0);
1762 p += strlen(p) + 1;
1764 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1765 infolen, 0, 0, 1, 0);
1766 p += strlen(p) + 1;
1768 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1769 inforellen, sec_symtab,
1770 is_elf64() ? debug_info : sec_debug_info,
1771 1, is_elf64() ? 24 : 12);
1772 p += strlen(p) + 1;
1774 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1775 abbrevlen, 0, 0, 1, 0);
1776 p += strlen(p) + 1;
1778 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1779 linelen, 0, 0, 1, 0);
1780 p += strlen(p) + 1;
1782 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1783 linerellen, sec_symtab,
1784 is_elf64() ? debug_line : sec_debug_line,
1785 1, is_elf64() ? 24 : 12);
1786 p += strlen(p) + 1;
1788 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1789 framelen, 0, 0, 8, 0);
1790 p += strlen(p) + 1;
1792 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1793 loclen, 0, 0, 1, 0);
1794 p += strlen(p) + 1;
1796 fwritezero(align, ofile);
1799 * Now output the sections.
1801 elf_write_sections();
1803 nasm_free(elf_sects);
1804 saa_free(symtab);
1807 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1809 struct SAA *s = saa_init(1L);
1810 struct elf_symbol *sym;
1811 uint8_t entry[24], *p;
1812 int i;
1814 *len = *local = 0;
1817 * First, an all-zeros entry, required by the ELF spec.
1819 saa_wbytes(s, NULL, is_elf64() ? 24L : 16L); /* null symbol table entry */
1820 *len += is_elf64() ? 24L : 16L;
1821 (*local)++;
1824 * Next, an entry for the file name.
1826 p = entry;
1827 if (is_elf64()) {
1828 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1829 WRITESHORT(p, STT_FILE); /* type FILE */
1830 WRITESHORT(p, SHN_ABS);
1831 WRITEDLONG(p, (uint64_t) 0); /* no value */
1832 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1833 saa_wbytes(s, entry, 24L);
1834 *len += 24;
1835 (*local)++;
1836 } else {
1837 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1838 WRITELONG(p, 0); /* no value */
1839 WRITELONG(p, 0); /* no size either */
1840 WRITESHORT(p, STT_FILE); /* type FILE */
1841 WRITESHORT(p, SHN_ABS);
1842 saa_wbytes(s, entry, 16L);
1843 *len += 16;
1844 (*local)++;
1848 * Now some standard symbols defining the segments, for relocation
1849 * purposes.
1851 if (is_elf64()) {
1852 for (i = 1; i <= nsects; i++) {
1853 p = entry;
1854 WRITELONG(p, 0); /* no symbol name */
1855 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1856 WRITESHORT(p, i); /* section id */
1857 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1858 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1859 saa_wbytes(s, entry, 24L);
1860 *len += 24;
1861 (*local)++;
1863 } else {
1864 for (i = 1; i <= nsects; i++) {
1865 p = entry;
1866 WRITELONG(p, 0); /* no symbol name */
1867 WRITELONG(p, 0); /* offset zero */
1868 WRITELONG(p, 0); /* size zero */
1869 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1870 WRITESHORT(p, i); /* section id */
1871 saa_wbytes(s, entry, 16L);
1872 *len += 16;
1873 (*local)++;
1878 * Now the other local symbols.
1880 saa_rewind(syms);
1881 if (is_elf64()) {
1882 while ((sym = saa_rstruct(syms))) {
1883 if (sym->type & SYM_GLOBAL)
1884 continue;
1885 p = entry;
1886 WRITELONG(p, sym->strpos); /* index into symbol string table */
1887 WRITECHAR(p, sym->type); /* type and binding */
1888 WRITECHAR(p, sym->other); /* visibility */
1889 WRITESHORT(p, sym->section); /* index into section header table */
1890 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1891 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1892 saa_wbytes(s, entry, 24L);
1893 *len += 24;
1894 (*local)++;
1897 * dwarf needs symbols for debug sections
1898 * which are relocation targets.
1900 if (dfmt == &df_dwarf) {
1901 dwarf_infosym = *local;
1902 p = entry;
1903 WRITELONG(p, 0); /* no symbol name */
1904 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1905 WRITESHORT(p, debug_info); /* section id */
1906 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1907 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1908 saa_wbytes(s, entry, 24L);
1909 *len += 24;
1910 (*local)++;
1911 dwarf_abbrevsym = *local;
1912 p = entry;
1913 WRITELONG(p, 0); /* no symbol name */
1914 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1915 WRITESHORT(p, debug_abbrev); /* section id */
1916 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1917 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1918 saa_wbytes(s, entry, 24L);
1919 *len += 24;
1920 (*local)++;
1921 dwarf_linesym = *local;
1922 p = entry;
1923 WRITELONG(p, 0); /* no symbol name */
1924 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1925 WRITESHORT(p, debug_line); /* section id */
1926 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1927 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1928 saa_wbytes(s, entry, 24L);
1929 *len += 24;
1930 (*local)++;
1932 } else {
1933 while ((sym = saa_rstruct(syms))) {
1934 if (sym->type & SYM_GLOBAL)
1935 continue;
1936 p = entry;
1937 WRITELONG(p, sym->strpos);
1938 WRITELONG(p, sym->symv.key);
1939 WRITELONG(p, sym->size);
1940 WRITECHAR(p, sym->type); /* type and binding */
1941 WRITECHAR(p, sym->other); /* visibility */
1942 WRITESHORT(p, sym->section);
1943 saa_wbytes(s, entry, 16L);
1944 *len += 16;
1945 (*local)++;
1948 * dwarf needs symbols for debug sections
1949 * which are relocation targets.
1951 if (dfmt == &df_dwarf) {
1952 dwarf_infosym = *local;
1953 p = entry;
1954 WRITELONG(p, 0); /* no symbol name */
1955 WRITELONG(p, (uint32_t) 0); /* offset zero */
1956 WRITELONG(p, (uint32_t) 0); /* size zero */
1957 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1958 WRITESHORT(p, sec_debug_info); /* section id */
1959 saa_wbytes(s, entry, 16L);
1960 *len += 16;
1961 (*local)++;
1962 dwarf_abbrevsym = *local;
1963 p = entry;
1964 WRITELONG(p, 0); /* no symbol name */
1965 WRITELONG(p, (uint32_t) 0); /* offset zero */
1966 WRITELONG(p, (uint32_t) 0); /* size zero */
1967 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1968 WRITESHORT(p, sec_debug_abbrev); /* section id */
1969 saa_wbytes(s, entry, 16L);
1970 *len += 16;
1971 (*local)++;
1972 dwarf_linesym = *local;
1973 p = entry;
1974 WRITELONG(p, 0); /* no symbol name */
1975 WRITELONG(p, (uint32_t) 0); /* offset zero */
1976 WRITELONG(p, (uint32_t) 0); /* size zero */
1977 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1978 WRITESHORT(p, sec_debug_line); /* section id */
1979 saa_wbytes(s, entry, 16L);
1980 *len += 16;
1981 (*local)++;
1986 * Now the global symbols.
1988 saa_rewind(syms);
1989 if (is_elf64()) {
1990 while ((sym = saa_rstruct(syms))) {
1991 if (!(sym->type & SYM_GLOBAL))
1992 continue;
1993 p = entry;
1994 WRITELONG(p, sym->strpos);
1995 WRITECHAR(p, sym->type); /* type and binding */
1996 WRITECHAR(p, sym->other); /* visibility */
1997 WRITESHORT(p, sym->section);
1998 WRITEDLONG(p, (int64_t)sym->symv.key);
1999 WRITEDLONG(p, (int64_t)sym->size);
2000 saa_wbytes(s, entry, 24L);
2001 *len += 24;
2003 } else {
2004 while ((sym = saa_rstruct(syms))) {
2005 if (!(sym->type & SYM_GLOBAL))
2006 continue;
2007 p = entry;
2008 WRITELONG(p, sym->strpos);
2009 WRITELONG(p, sym->symv.key);
2010 WRITELONG(p, sym->size);
2011 WRITECHAR(p, sym->type); /* type and binding */
2012 WRITECHAR(p, sym->other); /* visibility */
2013 WRITESHORT(p, sym->section);
2014 saa_wbytes(s, entry, 16L);
2015 *len += 16;
2019 return s;
2022 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
2024 struct SAA *s;
2025 uint8_t *p, entry[24];
2026 int32_t global_offset;
2028 if (!r)
2029 return NULL;
2031 s = saa_init(1L);
2032 *len = 0;
2035 * How to onvert from a global placeholder to a real symbol index;
2036 * the +2 refers to the two special entries, the null entry and
2037 * the filename entry.
2039 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
2041 if (is_elf32()) {
2042 while (r) {
2043 int32_t sym = r->symbol;
2045 if (sym >= GLOBAL_TEMP_BASE)
2046 sym += global_offset;
2048 p = entry;
2049 WRITELONG(p, r->address);
2050 WRITELONG(p, (sym << 8) + r->type);
2051 saa_wbytes(s, entry, 8L);
2052 *len += 8;
2054 r = r->next;
2056 } else if (is_elfx32()) {
2057 while (r) {
2058 int32_t sym = r->symbol;
2060 if (sym >= GLOBAL_TEMP_BASE)
2061 sym += global_offset;
2063 p = entry;
2064 WRITELONG(p, r->address);
2065 WRITELONG(p, (sym << 8) + r->type);
2066 WRITELONG(p, r->offset);
2067 saa_wbytes(s, entry, 12L);
2068 *len += 12;
2070 r = r->next;
2072 } else {
2073 nasm_assert(is_elf64());
2074 while (r) {
2075 int32_t sym = r->symbol;
2077 if (sym >= GLOBAL_TEMP_BASE)
2078 sym += global_offset;
2080 p = entry;
2081 WRITEDLONG(p, r->address);
2082 WRITELONG(p, r->type);
2083 WRITELONG(p, sym);
2084 WRITEDLONG(p, r->offset);
2085 saa_wbytes(s, entry, 24L);
2086 *len += 24;
2088 r = r->next;
2092 return s;
2095 static void elf_section_header(int name, int type, uint64_t flags,
2096 void *data, bool is_saa, uint64_t datalen,
2097 int link, int info, int align, int eltsize)
2099 elf_sects[elf_nsect].data = data;
2100 elf_sects[elf_nsect].len = datalen;
2101 elf_sects[elf_nsect].is_saa = is_saa;
2102 elf_nsect++;
2104 if (is_elf32()) {
2105 fwriteint32_t((int32_t)name, ofile);
2106 fwriteint32_t((int32_t)type, ofile);
2107 fwriteint32_t((int32_t)flags, ofile);
2108 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
2109 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
2110 fwriteint32_t(datalen, ofile);
2111 if (data)
2112 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2113 fwriteint32_t((int32_t)link, ofile);
2114 fwriteint32_t((int32_t)info, ofile);
2115 fwriteint32_t((int32_t)align, ofile);
2116 fwriteint32_t((int32_t)eltsize, ofile);
2117 } else if (is_elfx32()) {
2118 fwriteint32_t((int32_t)name, ofile);
2119 fwriteint32_t((int32_t)type, ofile);
2120 fwriteint32_t((int32_t)flags, ofile);
2121 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
2122 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
2123 fwriteint32_t(datalen, ofile);
2124 if (data)
2125 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2126 fwriteint32_t((int32_t)link, ofile);
2127 fwriteint32_t((int32_t)info, ofile);
2128 fwriteint32_t((int32_t)align, ofile);
2129 fwriteint32_t((int32_t)eltsize, ofile);
2130 } else {
2131 nasm_assert(is_elf64());
2132 fwriteint32_t((int32_t)name, ofile);
2133 fwriteint32_t((int32_t)type, ofile);
2134 fwriteint64_t((int64_t)flags, ofile);
2135 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
2136 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
2137 fwriteint64_t(datalen, ofile);
2138 if (data)
2139 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2140 fwriteint32_t((int32_t)link, ofile);
2141 fwriteint32_t((int32_t)info, ofile);
2142 fwriteint64_t((int64_t)align, ofile);
2143 fwriteint64_t((int64_t)eltsize, ofile);
2147 static void elf_write_sections(void)
2149 int i;
2150 for (i = 0; i < elf_nsect; i++)
2151 if (elf_sects[i].data) {
2152 int32_t len = elf_sects[i].len;
2153 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
2154 int32_t align = reallen - len;
2155 if (elf_sects[i].is_saa)
2156 saa_fpwrite(elf_sects[i].data, ofile);
2157 else
2158 nasm_write(elf_sects[i].data, len, ofile);
2159 fwritezero(align, ofile);
2163 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
2165 saa_wbytes(sect->data, data, len);
2166 sect->len += len;
2169 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
2171 saa_writeaddr(sect->data, data, len);
2172 sect->len += len;
2175 static void elf_sectalign(int32_t seg, unsigned int value)
2177 struct elf_section *s = NULL;
2178 int i;
2180 for (i = 0; i < nsects; i++) {
2181 if (sects[i]->index == seg) {
2182 s = sects[i];
2183 break;
2186 if (!s || !is_power2(value))
2187 return;
2189 if (value > s->align)
2190 s->align = value;
2193 static int32_t elf_segbase(int32_t segment)
2195 return segment;
2198 static void elf_filename(char *inname, char *outname)
2200 strcpy(elf_module, inname);
2201 standard_extension(inname, outname, ".o");
2204 extern macros_t elf_stdmac[];
2206 static int elf_set_info(enum geninfo type, char **val)
2208 (void)type;
2209 (void)val;
2210 return 0;
2213 static const struct dfmt elf32_df_dwarf = {
2214 "ELF32 (i386) dwarf debug format for Linux/Unix",
2215 "dwarf",
2216 dwarf_init,
2217 dwarf_linenum,
2218 null_debug_deflabel,
2219 null_debug_directive,
2220 debug_typevalue,
2221 dwarf_output,
2222 dwarf_cleanup
2225 static const struct dfmt elf32_df_stabs = {
2226 "ELF32 (i386) stabs debug format for Linux/Unix",
2227 "stabs",
2228 null_debug_init,
2229 stabs_linenum,
2230 null_debug_deflabel,
2231 null_debug_directive,
2232 debug_typevalue,
2233 stabs_output,
2234 stabs_cleanup
2237 static const struct dfmt * const elf32_debugs_arr[3] =
2238 { &elf32_df_dwarf, &elf32_df_stabs, NULL };
2240 const struct ofmt of_elf32 = {
2241 "ELF32 (i386) object files (e.g. Linux)",
2242 "elf32",
2245 elf32_debugs_arr,
2246 &elf32_df_stabs,
2247 elf_stdmac,
2248 elf_init,
2249 elf_set_info,
2250 elf32_out,
2251 elf_deflabel,
2252 elf_section_names,
2253 elf_sectalign,
2254 elf_segbase,
2255 elf_directive,
2256 elf_filename,
2257 elf_cleanup
2260 static const struct dfmt elf64_df_dwarf = {
2261 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
2262 "dwarf",
2263 dwarf_init,
2264 dwarf_linenum,
2265 null_debug_deflabel,
2266 null_debug_directive,
2267 debug_typevalue,
2268 dwarf_output,
2269 dwarf_cleanup
2272 static const struct dfmt elf64_df_stabs = {
2273 "ELF64 (x86-64) stabs debug format for Linux/Unix",
2274 "stabs",
2275 null_debug_init,
2276 stabs_linenum,
2277 null_debug_deflabel,
2278 null_debug_directive,
2279 debug_typevalue,
2280 stabs_output,
2281 stabs_cleanup
2284 static const struct dfmt * const elf64_debugs_arr[3] =
2285 { &elf64_df_dwarf, &elf64_df_stabs, NULL };
2287 const struct ofmt of_elf64 = {
2288 "ELF64 (x86_64) object files (e.g. Linux)",
2289 "elf64",
2292 elf64_debugs_arr,
2293 &elf64_df_stabs,
2294 elf_stdmac,
2295 elf_init,
2296 elf_set_info,
2297 elf64_out,
2298 elf_deflabel,
2299 elf_section_names,
2300 elf_sectalign,
2301 elf_segbase,
2302 elf_directive,
2303 elf_filename,
2304 elf_cleanup
2307 static const struct dfmt elfx32_df_dwarf = {
2308 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
2309 "dwarf",
2310 dwarf_init,
2311 dwarf_linenum,
2312 null_debug_deflabel,
2313 null_debug_directive,
2314 debug_typevalue,
2315 dwarf_output,
2316 dwarf_cleanup
2319 static const struct dfmt elfx32_df_stabs = {
2320 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
2321 "stabs",
2322 null_debug_init,
2323 stabs_linenum,
2324 null_debug_deflabel,
2325 null_debug_directive,
2326 debug_typevalue,
2327 stabs_output,
2328 stabs_cleanup
2331 static const struct dfmt * const elfx32_debugs_arr[3] =
2332 { &elfx32_df_dwarf, &elfx32_df_stabs, NULL };
2334 const struct ofmt of_elfx32 = {
2335 "ELFX32 (x86_64) object files (e.g. Linux)",
2336 "elfx32",
2339 elfx32_debugs_arr,
2340 &elfx32_df_stabs,
2341 elf_stdmac,
2342 elf_init,
2343 elf_set_info,
2344 elfx32_out,
2345 elf_deflabel,
2346 elf_section_names,
2347 elf_sectalign,
2348 elf_segbase,
2349 elf_directive,
2350 elf_filename,
2351 elf_cleanup
2354 static bool is_elf64(void)
2356 return ofmt == &of_elf64;
2359 static bool is_elf32(void)
2361 return ofmt == &of_elf32;
2364 static bool is_elfx32(void)
2366 return ofmt == &of_elfx32;
2369 /* common debugging routines */
2370 static void debug_typevalue(int32_t type)
2372 int32_t stype, ssize;
2373 switch (TYM_TYPE(type)) {
2374 case TY_LABEL:
2375 ssize = 0;
2376 stype = STT_NOTYPE;
2377 break;
2378 case TY_BYTE:
2379 ssize = 1;
2380 stype = STT_OBJECT;
2381 break;
2382 case TY_WORD:
2383 ssize = 2;
2384 stype = STT_OBJECT;
2385 break;
2386 case TY_DWORD:
2387 ssize = 4;
2388 stype = STT_OBJECT;
2389 break;
2390 case TY_FLOAT:
2391 ssize = 4;
2392 stype = STT_OBJECT;
2393 break;
2394 case TY_QWORD:
2395 ssize = 8;
2396 stype = STT_OBJECT;
2397 break;
2398 case TY_TBYTE:
2399 ssize = 10;
2400 stype = STT_OBJECT;
2401 break;
2402 case TY_OWORD:
2403 ssize = 16;
2404 stype = STT_OBJECT;
2405 break;
2406 case TY_YWORD:
2407 ssize = 32;
2408 stype = STT_OBJECT;
2409 break;
2410 case TY_COMMON:
2411 ssize = 0;
2412 stype = STT_COMMON;
2413 break;
2414 case TY_SEG:
2415 ssize = 0;
2416 stype = STT_SECTION;
2417 break;
2418 case TY_EXTERN:
2419 ssize = 0;
2420 stype = STT_NOTYPE;
2421 break;
2422 case TY_EQU:
2423 ssize = 0;
2424 stype = STT_NOTYPE;
2425 break;
2426 default:
2427 ssize = 0;
2428 stype = STT_NOTYPE;
2429 break;
2431 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
2432 lastsym->size = ssize;
2433 lastsym->type = stype;
2437 /* stabs debugging routines */
2439 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t segto)
2441 (void)segto;
2442 if (!stabs_filename) {
2443 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
2444 strcpy(stabs_filename, filename);
2445 } else {
2446 if (strcmp(stabs_filename, filename)) {
2447 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2448 in fact, this leak comes in quite handy to maintain a list of files
2449 encountered so far in the symbol lines... */
2451 /* why not nasm_free(stabs_filename); we're done with the old one */
2453 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
2454 strcpy(stabs_filename, filename);
2457 debug_immcall = 1;
2458 currentline = linenumber;
2461 static void stabs_output(int type, void *param)
2463 struct symlininfo *s;
2464 struct linelist *el;
2465 if (type == TY_DEBUGSYMLIN) {
2466 if (debug_immcall) {
2467 s = (struct symlininfo *)param;
2468 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2469 return; /* line info is only collected for executable sections */
2470 numlinestabs++;
2471 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2472 el->info.offset = s->offset;
2473 el->info.section = s->section;
2474 el->info.name = s->name;
2475 el->line = currentline;
2476 el->filename = stabs_filename;
2477 el->next = 0;
2478 if (stabslines) {
2479 stabslines->last->next = el;
2480 stabslines->last = el;
2481 } else {
2482 stabslines = el;
2483 stabslines->last = el;
2487 debug_immcall = 0;
2490 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2492 static void stabs_generate(void)
2494 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
2495 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
2496 char **allfiles;
2497 int *fileidx;
2499 struct linelist *ptr;
2501 ptr = stabslines;
2503 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
2504 numfiles = 0;
2505 while (ptr) {
2506 if (numfiles == 0) {
2507 allfiles[0] = ptr->filename;
2508 numfiles++;
2509 } else {
2510 for (i = 0; i < numfiles; i++) {
2511 if (!strcmp(allfiles[i], ptr->filename))
2512 break;
2514 if (i >= numfiles) {
2515 allfiles[i] = ptr->filename;
2516 numfiles++;
2519 ptr = ptr->next;
2521 strsize = 1;
2522 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
2523 for (i = 0; i < numfiles; i++) {
2524 fileidx[i] = strsize;
2525 strsize += strlen(allfiles[i]) + 1;
2527 mainfileindex = 0;
2528 for (i = 0; i < numfiles; i++) {
2529 if (!strcmp(allfiles[i], elf_module)) {
2530 mainfileindex = i;
2531 break;
2536 * worst case size of the stab buffer would be:
2537 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2538 * plus one "ending" entry
2540 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
2541 sizeof(struct stabentry));
2542 ssbuf = (uint8_t *)nasm_malloc(strsize);
2543 rbuf = (uint8_t *)nasm_malloc(numlinestabs * (is_elf64() ? 16 : 8) * (2 + 3));
2544 rptr = rbuf;
2546 for (i = 0; i < numfiles; i++)
2547 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
2548 ssbuf[0] = 0;
2550 stabstrlen = strsize; /* set global variable for length of stab strings */
2552 sptr = sbuf;
2553 ptr = stabslines;
2554 numstabs = 0;
2556 if (ptr) {
2558 * this is the first stab, its strx points to the filename of the
2559 * the source-file, the n_desc field should be set to the number
2560 * of remaining stabs
2562 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
2564 /* this is the stab for the main source file */
2565 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
2567 /* relocation table entry */
2570 * Since the symbol table has two entries before
2571 * the section symbols, the index in the info.section
2572 * member must be adjusted by adding 2
2575 if (is_elf32()) {
2576 WRITELONG(rptr, (sptr - sbuf) - 4);
2577 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2578 } else if (is_elfx32()) {
2579 WRITELONG(rptr, (sptr - sbuf) - 4);
2580 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2581 } else {
2582 nasm_assert(is_elf64());
2583 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2584 WRITELONG(rptr, R_X86_64_32);
2585 WRITELONG(rptr, ptr->info.section + 2);
2587 numstabs++;
2588 currfile = mainfileindex;
2591 if (is_elf32()) {
2592 while (ptr) {
2593 if (strcmp(allfiles[currfile], ptr->filename)) {
2594 /* oops file has changed... */
2595 for (i = 0; i < numfiles; i++)
2596 if (!strcmp(allfiles[i], ptr->filename))
2597 break;
2598 currfile = i;
2599 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2600 ptr->info.offset);
2601 numstabs++;
2603 /* relocation table entry */
2604 WRITELONG(rptr, (sptr - sbuf) - 4);
2605 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2608 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2609 numstabs++;
2611 /* relocation table entry */
2612 WRITELONG(rptr, (sptr - sbuf) - 4);
2613 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2615 ptr = ptr->next;
2617 } else if (is_elfx32()) {
2618 while (ptr) {
2619 if (strcmp(allfiles[currfile], ptr->filename)) {
2620 /* oops file has changed... */
2621 for (i = 0; i < numfiles; i++)
2622 if (!strcmp(allfiles[i], ptr->filename))
2623 break;
2624 currfile = i;
2625 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2626 ptr->info.offset);
2627 numstabs++;
2629 /* relocation table entry */
2630 WRITELONG(rptr, (sptr - sbuf) - 4);
2631 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2634 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2635 numstabs++;
2637 /* relocation table entry */
2638 WRITELONG(rptr, (sptr - sbuf) - 4);
2639 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2641 ptr = ptr->next;
2643 } else {
2644 nasm_assert(is_elf64());
2645 while (ptr) {
2646 if (strcmp(allfiles[currfile], ptr->filename)) {
2647 /* oops file has changed... */
2648 for (i = 0; i < numfiles; i++)
2649 if (!strcmp(allfiles[i], ptr->filename))
2650 break;
2651 currfile = i;
2652 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2653 ptr->info.offset);
2654 numstabs++;
2656 /* relocation table entry */
2657 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2658 WRITELONG(rptr, R_X86_64_32);
2659 WRITELONG(rptr, ptr->info.section + 2);
2662 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2663 numstabs++;
2665 /* relocation table entry */
2666 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2667 WRITELONG(rptr, R_X86_64_32);
2668 WRITELONG(rptr, ptr->info.section + 2);
2670 ptr = ptr->next;
2674 /* this is an "ending" token */
2675 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
2676 numstabs++;
2678 ((struct stabentry *)sbuf)->n_desc = numstabs;
2680 nasm_free(allfiles);
2681 nasm_free(fileidx);
2683 stablen = (sptr - sbuf);
2684 stabrellen = (rptr - rbuf);
2685 stabrelbuf = rbuf;
2686 stabbuf = sbuf;
2687 stabstrbuf = ssbuf;
2690 static void stabs_cleanup(void)
2692 struct linelist *ptr, *del;
2693 if (!stabslines)
2694 return;
2696 ptr = stabslines;
2697 while (ptr) {
2698 del = ptr;
2699 ptr = ptr->next;
2700 nasm_free(del);
2703 nasm_free(stabbuf);
2704 nasm_free(stabrelbuf);
2705 nasm_free(stabstrbuf);
2708 /* dwarf routines */
2710 static void dwarf_init(void)
2712 ndebugs = 3; /* 3 debug symbols */
2715 static void dwarf_linenum(const char *filename, int32_t linenumber,
2716 int32_t segto)
2718 (void)segto;
2719 dwarf_findfile(filename);
2720 debug_immcall = 1;
2721 currentline = linenumber;
2724 /* called from elf_out with type == TY_DEBUGSYMLIN */
2725 static void dwarf_output(int type, void *param)
2727 int ln, aa, inx, maxln, soc;
2728 struct symlininfo *s;
2729 struct SAA *plinep;
2731 (void)type;
2733 s = (struct symlininfo *)param;
2735 /* line number info is only gathered for executable sections */
2736 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2737 return;
2739 /* Check if section index has changed */
2740 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
2741 dwarf_findsect(s->section);
2743 /* do nothing unless line or file has changed */
2744 if (!debug_immcall)
2745 return;
2747 ln = currentline - dwarf_csect->line;
2748 aa = s->offset - dwarf_csect->offset;
2749 inx = dwarf_clist->line;
2750 plinep = dwarf_csect->psaa;
2751 /* check for file change */
2752 if (!(inx == dwarf_csect->file)) {
2753 saa_write8(plinep,DW_LNS_set_file);
2754 saa_write8(plinep,inx);
2755 dwarf_csect->file = inx;
2757 /* check for line change */
2758 if (ln) {
2759 /* test if in range of special op code */
2760 maxln = line_base + line_range;
2761 soc = (ln - line_base) + (line_range * aa) + opcode_base;
2762 if (ln >= line_base && ln < maxln && soc < 256) {
2763 saa_write8(plinep,soc);
2764 } else {
2765 saa_write8(plinep,DW_LNS_advance_line);
2766 saa_wleb128s(plinep,ln);
2767 if (aa) {
2768 saa_write8(plinep,DW_LNS_advance_pc);
2769 saa_wleb128u(plinep,aa);
2772 dwarf_csect->line = currentline;
2773 dwarf_csect->offset = s->offset;
2776 /* show change handled */
2777 debug_immcall = 0;
2781 static void dwarf_generate(void)
2783 uint8_t *pbuf;
2784 int indx;
2785 struct linelist *ftentry;
2786 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2787 struct SAA *parangesrel, *plinesrel, *pinforel;
2788 struct sectlist *psect;
2789 size_t saalen, linepoff, totlen, highaddr;
2791 if (is_elf32()) {
2792 /* write epilogues for each line program range */
2793 /* and build aranges section */
2794 paranges = saa_init(1L);
2795 parangesrel = saa_init(1L);
2796 saa_write16(paranges,2); /* dwarf version */
2797 saa_write32(parangesrel, paranges->datalen+4);
2798 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
2799 saa_write32(parangesrel, 0);
2800 saa_write32(paranges,0); /* offset into info */
2801 saa_write8(paranges,4); /* pointer size */
2802 saa_write8(paranges,0); /* not segmented */
2803 saa_write32(paranges,0); /* padding */
2804 /* iterate though sectlist entries */
2805 psect = dwarf_fsect;
2806 totlen = 0;
2807 highaddr = 0;
2808 for (indx = 0; indx < dwarf_nsections; indx++) {
2809 plinep = psect->psaa;
2810 /* Line Number Program Epilogue */
2811 saa_write8(plinep,2); /* std op 2 */
2812 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2813 saa_write8(plinep,DW_LNS_extended_op);
2814 saa_write8(plinep,1); /* operand length */
2815 saa_write8(plinep,DW_LNE_end_sequence);
2816 totlen += plinep->datalen;
2817 /* range table relocation entry */
2818 saa_write32(parangesrel, paranges->datalen + 4);
2819 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2820 saa_write32(parangesrel, (uint32_t) 0);
2821 /* range table entry */
2822 saa_write32(paranges,0x0000); /* range start */
2823 saa_write32(paranges,sects[psect->section]->len); /* range length */
2824 highaddr += sects[psect->section]->len;
2825 /* done with this entry */
2826 psect = psect->next;
2828 saa_write32(paranges,0); /* null address */
2829 saa_write32(paranges,0); /* null length */
2830 saalen = paranges->datalen;
2831 arangeslen = saalen + 4;
2832 arangesbuf = pbuf = nasm_malloc(arangeslen);
2833 WRITELONG(pbuf,saalen); /* initial length */
2834 saa_rnbytes(paranges, pbuf, saalen);
2835 saa_free(paranges);
2836 } else if (is_elfx32()) {
2837 /* write epilogues for each line program range */
2838 /* and build aranges section */
2839 paranges = saa_init(1L);
2840 parangesrel = saa_init(1L);
2841 saa_write16(paranges,3); /* dwarf version */
2842 saa_write32(parangesrel, paranges->datalen+4);
2843 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
2844 saa_write32(parangesrel, 0);
2845 saa_write32(paranges,0); /* offset into info */
2846 saa_write8(paranges,4); /* pointer size */
2847 saa_write8(paranges,0); /* not segmented */
2848 saa_write32(paranges,0); /* padding */
2849 /* iterate though sectlist entries */
2850 psect = dwarf_fsect;
2851 totlen = 0;
2852 highaddr = 0;
2853 for (indx = 0; indx < dwarf_nsections; indx++) {
2854 plinep = psect->psaa;
2855 /* Line Number Program Epilogue */
2856 saa_write8(plinep,2); /* std op 2 */
2857 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2858 saa_write8(plinep,DW_LNS_extended_op);
2859 saa_write8(plinep,1); /* operand length */
2860 saa_write8(plinep,DW_LNE_end_sequence);
2861 totlen += plinep->datalen;
2862 /* range table relocation entry */
2863 saa_write32(parangesrel, paranges->datalen + 4);
2864 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
2865 saa_write32(parangesrel, (uint32_t) 0);
2866 /* range table entry */
2867 saa_write32(paranges,0x0000); /* range start */
2868 saa_write32(paranges,sects[psect->section]->len); /* range length */
2869 highaddr += sects[psect->section]->len;
2870 /* done with this entry */
2871 psect = psect->next;
2873 saa_write32(paranges,0); /* null address */
2874 saa_write32(paranges,0); /* null length */
2875 saalen = paranges->datalen;
2876 arangeslen = saalen + 4;
2877 arangesbuf = pbuf = nasm_malloc(arangeslen);
2878 WRITELONG(pbuf,saalen); /* initial length */
2879 saa_rnbytes(paranges, pbuf, saalen);
2880 saa_free(paranges);
2881 } else {
2882 nasm_assert(is_elf64());
2883 /* write epilogues for each line program range */
2884 /* and build aranges section */
2885 paranges = saa_init(1L);
2886 parangesrel = saa_init(1L);
2887 saa_write16(paranges,3); /* dwarf version */
2888 saa_write64(parangesrel, paranges->datalen+4);
2889 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2890 saa_write64(parangesrel, 0);
2891 saa_write32(paranges,0); /* offset into info */
2892 saa_write8(paranges,8); /* pointer size */
2893 saa_write8(paranges,0); /* not segmented */
2894 saa_write32(paranges,0); /* padding */
2895 /* iterate though sectlist entries */
2896 psect = dwarf_fsect;
2897 totlen = 0;
2898 highaddr = 0;
2899 for (indx = 0; indx < dwarf_nsections; indx++) {
2900 plinep = psect->psaa;
2901 /* Line Number Program Epilogue */
2902 saa_write8(plinep,2); /* std op 2 */
2903 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2904 saa_write8(plinep,DW_LNS_extended_op);
2905 saa_write8(plinep,1); /* operand length */
2906 saa_write8(plinep,DW_LNE_end_sequence);
2907 totlen += plinep->datalen;
2908 /* range table relocation entry */
2909 saa_write64(parangesrel, paranges->datalen + 4);
2910 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2911 saa_write64(parangesrel, (uint64_t) 0);
2912 /* range table entry */
2913 saa_write64(paranges,0x0000); /* range start */
2914 saa_write64(paranges,sects[psect->section]->len); /* range length */
2915 highaddr += sects[psect->section]->len;
2916 /* done with this entry */
2917 psect = psect->next;
2919 saa_write64(paranges,0); /* null address */
2920 saa_write64(paranges,0); /* null length */
2921 saalen = paranges->datalen;
2922 arangeslen = saalen + 4;
2923 arangesbuf = pbuf = nasm_malloc(arangeslen);
2924 WRITELONG(pbuf,saalen); /* initial length */
2925 saa_rnbytes(paranges, pbuf, saalen);
2926 saa_free(paranges);
2929 /* build rela.aranges section */
2930 arangesrellen = saalen = parangesrel->datalen;
2931 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2932 saa_rnbytes(parangesrel, pbuf, saalen);
2933 saa_free(parangesrel);
2935 /* build pubnames section */
2936 ppubnames = saa_init(1L);
2937 saa_write16(ppubnames,3); /* dwarf version */
2938 saa_write32(ppubnames,0); /* offset into info */
2939 saa_write32(ppubnames,0); /* space used in info */
2940 saa_write32(ppubnames,0); /* end of list */
2941 saalen = ppubnames->datalen;
2942 pubnameslen = saalen + 4;
2943 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2944 WRITELONG(pbuf,saalen); /* initial length */
2945 saa_rnbytes(ppubnames, pbuf, saalen);
2946 saa_free(ppubnames);
2948 if (is_elf32()) {
2949 /* build info section */
2950 pinfo = saa_init(1L);
2951 pinforel = saa_init(1L);
2952 saa_write16(pinfo,2); /* dwarf version */
2953 saa_write32(pinforel, pinfo->datalen + 4);
2954 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
2955 saa_write32(pinforel, 0);
2956 saa_write32(pinfo,0); /* offset into abbrev */
2957 saa_write8(pinfo,4); /* pointer size */
2958 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2959 saa_write32(pinforel, pinfo->datalen + 4);
2960 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2961 saa_write32(pinforel, 0);
2962 saa_write32(pinfo,0); /* DW_AT_low_pc */
2963 saa_write32(pinforel, pinfo->datalen + 4);
2964 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2965 saa_write32(pinforel, 0);
2966 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
2967 saa_write32(pinforel, pinfo->datalen + 4);
2968 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
2969 saa_write32(pinforel, 0);
2970 saa_write32(pinfo,0); /* DW_AT_stmt_list */
2971 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
2972 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
2973 saa_write16(pinfo,DW_LANG_Mips_Assembler);
2974 saa_write8(pinfo,2); /* abbrviation number LEB128u */
2975 saa_write32(pinforel, pinfo->datalen + 4);
2976 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2977 saa_write32(pinforel, 0);
2978 saa_write32(pinfo,0); /* DW_AT_low_pc */
2979 saa_write32(pinfo,0); /* DW_AT_frame_base */
2980 saa_write8(pinfo,0); /* end of entries */
2981 saalen = pinfo->datalen;
2982 infolen = saalen + 4;
2983 infobuf = pbuf = nasm_malloc(infolen);
2984 WRITELONG(pbuf,saalen); /* initial length */
2985 saa_rnbytes(pinfo, pbuf, saalen);
2986 saa_free(pinfo);
2987 } else if (is_elfx32()) {
2988 /* build info section */
2989 pinfo = saa_init(1L);
2990 pinforel = saa_init(1L);
2991 saa_write16(pinfo,3); /* dwarf version */
2992 saa_write32(pinforel, pinfo->datalen + 4);
2993 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
2994 saa_write32(pinforel, 0);
2995 saa_write32(pinfo,0); /* offset into abbrev */
2996 saa_write8(pinfo,4); /* pointer size */
2997 saa_write8(pinfo,1); /* abbrviation number LEB128u */
2998 saa_write32(pinforel, pinfo->datalen + 4);
2999 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3000 saa_write32(pinforel, 0);
3001 saa_write32(pinfo,0); /* DW_AT_low_pc */
3002 saa_write32(pinforel, pinfo->datalen + 4);
3003 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3004 saa_write32(pinforel, 0);
3005 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
3006 saa_write32(pinforel, pinfo->datalen + 4);
3007 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
3008 saa_write32(pinforel, 0);
3009 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3010 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3011 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3012 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3013 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3014 saa_write32(pinforel, pinfo->datalen + 4);
3015 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3016 saa_write32(pinforel, 0);
3017 saa_write32(pinfo,0); /* DW_AT_low_pc */
3018 saa_write32(pinfo,0); /* DW_AT_frame_base */
3019 saa_write8(pinfo,0); /* end of entries */
3020 saalen = pinfo->datalen;
3021 infolen = saalen + 4;
3022 infobuf = pbuf = nasm_malloc(infolen);
3023 WRITELONG(pbuf,saalen); /* initial length */
3024 saa_rnbytes(pinfo, pbuf, saalen);
3025 saa_free(pinfo);
3026 } else {
3027 nasm_assert(is_elf64());
3028 /* build info section */
3029 pinfo = saa_init(1L);
3030 pinforel = saa_init(1L);
3031 saa_write16(pinfo,3); /* dwarf version */
3032 saa_write64(pinforel, pinfo->datalen + 4);
3033 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
3034 saa_write64(pinforel, 0);
3035 saa_write32(pinfo,0); /* offset into abbrev */
3036 saa_write8(pinfo,8); /* pointer size */
3037 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3038 saa_write64(pinforel, pinfo->datalen + 4);
3039 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3040 saa_write64(pinforel, 0);
3041 saa_write64(pinfo,0); /* DW_AT_low_pc */
3042 saa_write64(pinforel, pinfo->datalen + 4);
3043 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3044 saa_write64(pinforel, 0);
3045 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
3046 saa_write64(pinforel, pinfo->datalen + 4);
3047 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
3048 saa_write64(pinforel, 0);
3049 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3050 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3051 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3052 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3053 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3054 saa_write64(pinforel, pinfo->datalen + 4);
3055 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3056 saa_write64(pinforel, 0);
3057 saa_write64(pinfo,0); /* DW_AT_low_pc */
3058 saa_write64(pinfo,0); /* DW_AT_frame_base */
3059 saa_write8(pinfo,0); /* end of entries */
3060 saalen = pinfo->datalen;
3061 infolen = saalen + 4;
3062 infobuf = pbuf = nasm_malloc(infolen);
3063 WRITELONG(pbuf,saalen); /* initial length */
3064 saa_rnbytes(pinfo, pbuf, saalen);
3065 saa_free(pinfo);
3068 /* build rela.info section */
3069 inforellen = saalen = pinforel->datalen;
3070 inforelbuf = pbuf = nasm_malloc(inforellen);
3071 saa_rnbytes(pinforel, pbuf, saalen);
3072 saa_free(pinforel);
3074 /* build abbrev section */
3075 pabbrev = saa_init(1L);
3076 saa_write8(pabbrev,1); /* entry number LEB128u */
3077 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
3078 saa_write8(pabbrev,1); /* has children */
3079 /* the following attributes and forms are all LEB128u values */
3080 saa_write8(pabbrev,DW_AT_low_pc);
3081 saa_write8(pabbrev,DW_FORM_addr);
3082 saa_write8(pabbrev,DW_AT_high_pc);
3083 saa_write8(pabbrev,DW_FORM_addr);
3084 saa_write8(pabbrev,DW_AT_stmt_list);
3085 saa_write8(pabbrev,DW_FORM_data4);
3086 saa_write8(pabbrev,DW_AT_name);
3087 saa_write8(pabbrev,DW_FORM_string);
3088 saa_write8(pabbrev,DW_AT_producer);
3089 saa_write8(pabbrev,DW_FORM_string);
3090 saa_write8(pabbrev,DW_AT_language);
3091 saa_write8(pabbrev,DW_FORM_data2);
3092 saa_write16(pabbrev,0); /* end of entry */
3093 /* LEB128u usage same as above */
3094 saa_write8(pabbrev,2); /* entry number */
3095 saa_write8(pabbrev,DW_TAG_subprogram);
3096 saa_write8(pabbrev,0); /* no children */
3097 saa_write8(pabbrev,DW_AT_low_pc);
3098 saa_write8(pabbrev,DW_FORM_addr);
3099 saa_write8(pabbrev,DW_AT_frame_base);
3100 saa_write8(pabbrev,DW_FORM_data4);
3101 saa_write16(pabbrev,0); /* end of entry */
3102 abbrevlen = saalen = pabbrev->datalen;
3103 abbrevbuf = pbuf = nasm_malloc(saalen);
3104 saa_rnbytes(pabbrev, pbuf, saalen);
3105 saa_free(pabbrev);
3107 /* build line section */
3108 /* prolog */
3109 plines = saa_init(1L);
3110 saa_write8(plines,1); /* Minimum Instruction Length */
3111 saa_write8(plines,1); /* Initial value of 'is_stmt' */
3112 saa_write8(plines,line_base); /* Line Base */
3113 saa_write8(plines,line_range); /* Line Range */
3114 saa_write8(plines,opcode_base); /* Opcode Base */
3115 /* standard opcode lengths (# of LEB128u operands) */
3116 saa_write8(plines,0); /* Std opcode 1 length */
3117 saa_write8(plines,1); /* Std opcode 2 length */
3118 saa_write8(plines,1); /* Std opcode 3 length */
3119 saa_write8(plines,1); /* Std opcode 4 length */
3120 saa_write8(plines,1); /* Std opcode 5 length */
3121 saa_write8(plines,0); /* Std opcode 6 length */
3122 saa_write8(plines,0); /* Std opcode 7 length */
3123 saa_write8(plines,0); /* Std opcode 8 length */
3124 saa_write8(plines,1); /* Std opcode 9 length */
3125 saa_write8(plines,0); /* Std opcode 10 length */
3126 saa_write8(plines,0); /* Std opcode 11 length */
3127 saa_write8(plines,1); /* Std opcode 12 length */
3128 /* Directory Table */
3129 saa_write8(plines,0); /* End of table */
3130 /* File Name Table */
3131 ftentry = dwarf_flist;
3132 for (indx = 0; indx < dwarf_numfiles; indx++) {
3133 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
3134 saa_write8(plines,0); /* directory LEB128u */
3135 saa_write8(plines,0); /* time LEB128u */
3136 saa_write8(plines,0); /* size LEB128u */
3137 ftentry = ftentry->next;
3139 saa_write8(plines,0); /* End of table */
3140 linepoff = plines->datalen;
3141 linelen = linepoff + totlen + 10;
3142 linebuf = pbuf = nasm_malloc(linelen);
3143 WRITELONG(pbuf,linelen-4); /* initial length */
3144 WRITESHORT(pbuf,3); /* dwarf version */
3145 WRITELONG(pbuf,linepoff); /* offset to line number program */
3146 /* write line header */
3147 saalen = linepoff;
3148 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
3149 pbuf += linepoff;
3150 saa_free(plines);
3151 /* concatonate line program ranges */
3152 linepoff += 13;
3153 plinesrel = saa_init(1L);
3154 psect = dwarf_fsect;
3155 if (is_elf32()) {
3156 for (indx = 0; indx < dwarf_nsections; indx++) {
3157 saa_write32(plinesrel, linepoff);
3158 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
3159 saa_write32(plinesrel, (uint32_t) 0);
3160 plinep = psect->psaa;
3161 saalen = plinep->datalen;
3162 saa_rnbytes(plinep, pbuf, saalen);
3163 pbuf += saalen;
3164 linepoff += saalen;
3165 saa_free(plinep);
3166 /* done with this entry */
3167 psect = psect->next;
3169 } else if (is_elfx32()) {
3170 for (indx = 0; indx < dwarf_nsections; indx++) {
3171 saa_write32(plinesrel, linepoff);
3172 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
3173 saa_write32(plinesrel, 0);
3174 plinep = psect->psaa;
3175 saalen = plinep->datalen;
3176 saa_rnbytes(plinep, pbuf, saalen);
3177 pbuf += saalen;
3178 linepoff += saalen;
3179 saa_free(plinep);
3180 /* done with this entry */
3181 psect = psect->next;
3183 } else {
3184 nasm_assert(is_elf64());
3185 for (indx = 0; indx < dwarf_nsections; indx++) {
3186 saa_write64(plinesrel, linepoff);
3187 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
3188 saa_write64(plinesrel, (uint64_t) 0);
3189 plinep = psect->psaa;
3190 saalen = plinep->datalen;
3191 saa_rnbytes(plinep, pbuf, saalen);
3192 pbuf += saalen;
3193 linepoff += saalen;
3194 saa_free(plinep);
3195 /* done with this entry */
3196 psect = psect->next;
3200 /* build rela.lines section */
3201 linerellen =saalen = plinesrel->datalen;
3202 linerelbuf = pbuf = nasm_malloc(linerellen);
3203 saa_rnbytes(plinesrel, pbuf, saalen);
3204 saa_free(plinesrel);
3206 /* build frame section */
3207 framelen = 4;
3208 framebuf = pbuf = nasm_malloc(framelen);
3209 WRITELONG(pbuf,framelen-4); /* initial length */
3211 /* build loc section */
3212 loclen = 16;
3213 locbuf = pbuf = nasm_malloc(loclen);
3214 if (is_elf32()) {
3215 WRITELONG(pbuf,0); /* null beginning offset */
3216 WRITELONG(pbuf,0); /* null ending offset */
3217 } else {
3218 nasm_assert(is_elf64());
3219 WRITEDLONG(pbuf,0); /* null beginning offset */
3220 WRITEDLONG(pbuf,0); /* null ending offset */
3224 static void dwarf_cleanup(void)
3226 nasm_free(arangesbuf);
3227 nasm_free(arangesrelbuf);
3228 nasm_free(pubnamesbuf);
3229 nasm_free(infobuf);
3230 nasm_free(inforelbuf);
3231 nasm_free(abbrevbuf);
3232 nasm_free(linebuf);
3233 nasm_free(linerelbuf);
3234 nasm_free(framebuf);
3235 nasm_free(locbuf);
3238 static void dwarf_findfile(const char * fname)
3240 int finx;
3241 struct linelist *match;
3243 /* return if fname is current file name */
3244 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
3245 return;
3247 /* search for match */
3248 match = 0;
3249 if (dwarf_flist) {
3250 match = dwarf_flist;
3251 for (finx = 0; finx < dwarf_numfiles; finx++) {
3252 if (!(strcmp(fname, match->filename))) {
3253 dwarf_clist = match;
3254 return;
3259 /* add file name to end of list */
3260 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
3261 dwarf_numfiles++;
3262 dwarf_clist->line = dwarf_numfiles;
3263 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
3264 strcpy(dwarf_clist->filename,fname);
3265 dwarf_clist->next = 0;
3266 if (!dwarf_flist) { /* if first entry */
3267 dwarf_flist = dwarf_elist = dwarf_clist;
3268 dwarf_clist->last = 0;
3269 } else { /* chain to previous entry */
3270 dwarf_elist->next = dwarf_clist;
3271 dwarf_elist = dwarf_clist;
3275 static void dwarf_findsect(const int index)
3277 int sinx;
3278 struct sectlist *match;
3279 struct SAA *plinep;
3281 /* return if index is current section index */
3282 if (dwarf_csect && (dwarf_csect->section == index))
3283 return;
3285 /* search for match */
3286 match = 0;
3287 if (dwarf_fsect) {
3288 match = dwarf_fsect;
3289 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
3290 if (match->section == index) {
3291 dwarf_csect = match;
3292 return;
3294 match = match->next;
3298 /* add entry to end of list */
3299 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
3300 dwarf_nsections++;
3301 dwarf_csect->psaa = plinep = saa_init(1L);
3302 dwarf_csect->line = 1;
3303 dwarf_csect->offset = 0;
3304 dwarf_csect->file = 1;
3305 dwarf_csect->section = index;
3306 dwarf_csect->next = 0;
3307 /* set relocatable address at start of line program */
3308 saa_write8(plinep,DW_LNS_extended_op);
3309 saa_write8(plinep,is_elf64() ? 9 : 5); /* operand length */
3310 saa_write8(plinep,DW_LNE_set_address);
3311 if (is_elf64())
3312 saa_write64(plinep,0); /* Start Address */
3313 else
3314 saa_write32(plinep,0); /* Start Address */
3316 if (!dwarf_fsect) { /* if first entry */
3317 dwarf_fsect = dwarf_esect = dwarf_csect;
3318 dwarf_csect->last = 0;
3319 } else { /* chain to previous entry */
3320 dwarf_esect->next = dwarf_csect;
3321 dwarf_esect = dwarf_csect;
3325 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */