output: remove ABSOLUTE handling, OUT_RAWDATA asserts
[nasm.git] / output / outelf.c
blob34c88361c631787ba34f5134c2f98bd92f236dc8
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * Common code for outelf32 and outelf64
38 #include "compiler.h"
40 #include <stdio.h>
41 #include <stdlib.h>
43 #include "nasm.h"
44 #include "nasmlib.h"
45 #include "error.h"
46 #include "saa.h"
47 #include "raa.h"
48 #include "stdscan.h"
49 #include "eval.h"
50 #include "outform.h"
51 #include "outlib.h"
52 #include "rbtree.h"
53 #include "ver.h"
55 #include "dwarf.h"
56 #include "stabs.h"
57 #include "outelf.h"
58 #include "elf.h"
60 #if defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32)
62 #define SECT_DELTA 32
63 static struct elf_section **sects;
64 static int nsects, sectlen;
66 #define SHSTR_DELTA 256
67 static char *shstrtab;
68 static int shstrtablen, shstrtabsize;
70 static struct SAA *syms;
71 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
73 static int32_t def_seg;
75 static struct RAA *bsym;
77 static struct SAA *strs;
78 static uint32_t strslen;
80 static struct elf_symbol *fwds;
82 static char elf_module[FILENAME_MAX];
84 extern const struct ofmt of_elf32;
85 extern const struct ofmt of_elf64;
86 extern const struct ofmt of_elfx32;
88 static struct ELF_SECTDATA {
89 void *data;
90 int64_t len;
91 bool is_saa;
92 } *elf_sects;
94 static int elf_nsect, nsections;
95 static int64_t elf_foffs;
97 static void elf_write(void);
98 static void elf_sect_write(struct elf_section *, const void *, size_t);
99 static void elf_sect_writeaddr(struct elf_section *, int64_t, size_t);
100 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
101 int, int);
102 static void elf_write_sections(void);
103 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
104 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
105 static void add_sectname(const char *, const char *);
107 struct erel {
108 int offset;
109 int info;
112 struct symlininfo {
113 int offset;
114 int section; /* index into sects[] */
115 int segto; /* internal section number */
116 char *name; /* shallow-copied pointer of section name */
119 struct linelist {
120 struct linelist *next;
121 struct linelist *last;
122 struct symlininfo info;
123 char *filename;
124 int line;
127 struct sectlist {
128 struct SAA *psaa;
129 int section;
130 int line;
131 int offset;
132 int file;
133 struct sectlist *next;
134 struct sectlist *last;
137 /* common debug variables */
138 static int currentline = 1;
139 static int debug_immcall = 0;
141 /* stabs debug variables */
142 static struct linelist *stabslines = 0;
143 static int numlinestabs = 0;
144 static char *stabs_filename = 0;
145 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
146 static int stablen, stabstrlen, stabrellen;
148 /* dwarf debug variables */
149 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
150 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
151 static int dwarf_numfiles = 0, dwarf_nsections;
152 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
153 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
154 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
155 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
156 abbrevlen, linelen, linerellen, framelen, loclen;
157 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
159 static struct elf_symbol *lastsym;
161 /* common debugging routines */
162 static void debug_typevalue(int32_t);
164 /* stabs debugging routines */
165 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t);
166 static void stabs_output(int, void *);
167 static void stabs_generate(void);
168 static void stabs_cleanup(void);
170 /* dwarf debugging routines */
171 static void dwarf_init(void);
172 static void dwarf_linenum(const char *filename, int32_t linenumber, int32_t);
173 static void dwarf_output(int, void *);
174 static void dwarf_generate(void);
175 static void dwarf_cleanup(void);
176 static void dwarf_findfile(const char *);
177 static void dwarf_findsect(const int);
179 static bool is_elf64(void);
180 static bool is_elf32(void);
181 static bool is_elfx32(void);
183 static bool dfmt_is_stabs(void);
184 static bool dfmt_is_dwarf(void);
187 * Special NASM section numbers which are used to define ELF special
188 * symbols.
190 static int32_t elf_gotpc_sect, elf_gotoff_sect;
191 static int32_t elf_got_sect, elf_plt_sect;
192 static int32_t elf_sym_sect, elf_gottpoff_sect, elf_tlsie_sect;
194 uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
195 uint8_t elf_abiver = 0; /* Current ABI version */
197 const struct elf_known_section elf_known_sections[] = {
198 { ".text", SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, 16 },
199 { ".rodata", SHT_PROGBITS, SHF_ALLOC, 4 },
200 { ".lrodata", SHT_PROGBITS, SHF_ALLOC, 4 },
201 { ".data", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4 },
202 { ".ldata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 4 },
203 { ".bss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4 },
204 { ".lbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE, 4 },
205 { ".tdata", SHT_PROGBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4 },
206 { ".tbss", SHT_NOBITS, SHF_ALLOC|SHF_WRITE|SHF_TLS, 4 },
207 { ".comment", SHT_PROGBITS, 0, 1 },
208 { NULL, SHT_PROGBITS, SHF_ALLOC, 1 } /* default */
211 /* parse section attributes */
212 static void elf_section_attrib(char *name, char *attr, int pass,
213 uint32_t *flags_and, uint32_t *flags_or,
214 uint64_t *align, int *type)
216 char *opt, *val, *next;
218 opt = nasm_skip_spaces(attr);
219 if (!opt || !*opt)
220 return;
222 while ((opt = nasm_opt_val(opt, &val, &next))) {
223 if (!nasm_stricmp(opt, "align")) {
224 if (!val) {
225 nasm_error(ERR_NONFATAL,
226 "section align without value specified");
227 } else {
228 *align = atoi(val);
229 if (*align == 0) {
230 *align = SHA_ANY;
231 } else if (!is_power2(*align)) {
232 nasm_error(ERR_NONFATAL,
233 "section alignment %"PRId64" is not a power of two",
234 *align);
235 *align = SHA_ANY;
238 } else if (!nasm_stricmp(opt, "alloc")) {
239 *flags_and |= SHF_ALLOC;
240 *flags_or |= SHF_ALLOC;
241 } else if (!nasm_stricmp(opt, "noalloc")) {
242 *flags_and |= SHF_ALLOC;
243 *flags_or &= ~SHF_ALLOC;
244 } else if (!nasm_stricmp(opt, "exec")) {
245 *flags_and |= SHF_EXECINSTR;
246 *flags_or |= SHF_EXECINSTR;
247 } else if (!nasm_stricmp(opt, "noexec")) {
248 *flags_and |= SHF_EXECINSTR;
249 *flags_or &= ~SHF_EXECINSTR;
250 } else if (!nasm_stricmp(opt, "write")) {
251 *flags_and |= SHF_WRITE;
252 *flags_or |= SHF_WRITE;
253 } else if (!nasm_stricmp(opt, "tls")) {
254 *flags_and |= SHF_TLS;
255 *flags_or |= SHF_TLS;
256 } else if (!nasm_stricmp(opt, "nowrite")) {
257 *flags_and |= SHF_WRITE;
258 *flags_or &= ~SHF_WRITE;
259 } else if (!nasm_stricmp(opt, "progbits")) {
260 *type = SHT_PROGBITS;
261 } else if (!nasm_stricmp(opt, "nobits")) {
262 *type = SHT_NOBITS;
263 } else if (pass == 1) {
264 nasm_error(ERR_WARNING,
265 "Unknown section attribute '%s' ignored on"
266 " declaration of section `%s'", opt, name);
268 opt = next;
272 static enum directive_result
273 elf_directive(enum directive directive, char *value, int pass)
275 int64_t n;
276 bool err;
277 char *p;
279 switch (directive) {
280 case D_OSABI:
281 if (pass == 2)
282 return DIRR_OK; /* ignore in pass 2 */
284 n = readnum(value, &err);
285 if (err) {
286 nasm_error(ERR_NONFATAL, "`osabi' directive requires a parameter");
287 return DIRR_ERROR;
290 if (n < 0 || n > 255) {
291 nasm_error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
292 return DIRR_ERROR;
295 elf_osabi = n;
296 elf_abiver = 0;
298 p = strchr(value,',');
299 if (!p)
300 return DIRR_OK;
302 n = readnum(p + 1, &err);
303 if (err || n < 0 || n > 255) {
304 nasm_error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
305 return DIRR_ERROR;
308 elf_abiver = n;
309 return DIRR_OK;
311 default:
312 return DIRR_UNKNOWN;
316 static void elf_init(void)
318 strlcpy(elf_module, inname, sizeof(elf_module));
319 sects = NULL;
320 nsects = sectlen = 0;
321 syms = saa_init((int32_t)sizeof(struct elf_symbol));
322 nlocals = nglobs = ndebugs = 0;
323 bsym = raa_init();
324 strs = saa_init(1L);
325 saa_wbytes(strs, "\0", 1L);
326 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
327 strslen = 2 + strlen(elf_module);
328 shstrtab = NULL;
329 shstrtablen = shstrtabsize = 0;;
330 add_sectname("", "");
332 fwds = NULL;
335 * FIXME: tlsie is Elf32 only and
336 * gottpoff is Elfx32|64 only.
339 elf_gotpc_sect = seg_alloc();
340 backend_label("..gotpc", elf_gotpc_sect + 1, 0L);
341 elf_gotoff_sect = seg_alloc();
342 backend_label("..gotoff", elf_gotoff_sect + 1, 0L);
343 elf_got_sect = seg_alloc();
344 backend_label("..got", elf_got_sect + 1, 0L);
345 elf_plt_sect = seg_alloc();
346 backend_label("..plt", elf_plt_sect + 1, 0L);
347 elf_sym_sect = seg_alloc();
348 backend_label("..sym", elf_sym_sect + 1, 0L);
349 elf_gottpoff_sect = seg_alloc();
350 backend_label("..gottpoff", elf_gottpoff_sect + 1, 0L);
351 elf_tlsie_sect = seg_alloc();
352 backend_label("..tlsie", elf_tlsie_sect + 1, 0L);
354 def_seg = seg_alloc();
357 static void elf_cleanup(void)
359 struct elf_reloc *r;
360 int i;
362 elf_write();
363 for (i = 0; i < nsects; i++) {
364 if (sects[i]->type != SHT_NOBITS)
365 saa_free(sects[i]->data);
366 if (sects[i]->head)
367 saa_free(sects[i]->rel);
368 while (sects[i]->head) {
369 r = sects[i]->head;
370 sects[i]->head = sects[i]->head->next;
371 nasm_free(r);
374 nasm_free(sects);
375 saa_free(syms);
376 raa_free(bsym);
377 saa_free(strs);
378 dfmt->cleanup();
381 /* add entry to the elf .shstrtab section */
382 static void add_sectname(const char *firsthalf, const char *secondhalf)
384 int len = strlen(firsthalf) + strlen(secondhalf);
385 while (shstrtablen + len + 1 > shstrtabsize)
386 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
387 strcpy(shstrtab + shstrtablen, firsthalf);
388 strcat(shstrtab + shstrtablen, secondhalf);
389 shstrtablen += len + 1;
392 static int elf_make_section(char *name, int type, int flags, int align)
394 struct elf_section *s;
396 s = nasm_zalloc(sizeof(*s));
398 if (type != SHT_NOBITS)
399 s->data = saa_init(1L);
400 s->tail = &s->head;
401 if (!strcmp(name, ".text"))
402 s->index = def_seg;
403 else
404 s->index = seg_alloc();
405 add_sectname("", name);
407 s->name = nasm_strdup(name);
408 s->type = type;
409 s->flags = flags;
410 s->align = align;
412 if (nsects >= sectlen)
413 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
414 sects[nsects++] = s;
416 return nsects - 1;
419 static int32_t elf_section_names(char *name, int pass, int *bits)
421 char *p;
422 uint32_t flags, flags_and, flags_or;
423 uint64_t align;
424 int type, i;
426 if (!name) {
427 *bits = ofmt->maxbits;
428 return def_seg;
431 p = nasm_skip_word(name);
432 if (*p)
433 *p++ = '\0';
434 flags_and = flags_or = type = align = 0;
436 elf_section_attrib(name, p, pass, &flags_and,
437 &flags_or, &align, &type);
439 if (!strcmp(name, ".shstrtab") ||
440 !strcmp(name, ".symtab") ||
441 !strcmp(name, ".strtab")) {
442 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
443 "name `%s'", name);
444 return NO_SEG;
447 for (i = 0; i < nsects; i++)
448 if (!strcmp(name, sects[i]->name))
449 break;
450 if (i == nsects) {
451 const struct elf_known_section *ks = elf_known_sections;
453 while (ks->name) {
454 if (!strcmp(name, ks->name))
455 break;
456 ks++;
459 type = type ? type : ks->type;
460 align = align ? align : ks->align;
461 flags = (ks->flags & ~flags_and) | flags_or;
463 i = elf_make_section(name, type, flags, align);
464 } else if (pass == 1) {
465 if ((type && sects[i]->type != type)
466 || (align && sects[i]->align != align)
467 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
468 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
469 " redeclaration of section `%s'", name);
472 return sects[i]->index;
475 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
476 int is_global, char *special)
478 int pos = strslen;
479 struct elf_symbol *sym;
480 bool special_used = false;
482 #if defined(DEBUG) && DEBUG>2
483 nasm_error(ERR_DEBUG,
484 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
485 name, segment, offset, is_global, special);
486 #endif
487 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
489 * This is a NASM special symbol. We never allow it into
490 * the ELF symbol table, even if it's a valid one. If it
491 * _isn't_ a valid one, we should barf immediately.
493 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
495 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
496 strcmp(name, "..got") && strcmp(name, "..plt") &&
497 strcmp(name, "..sym") && strcmp(name, "..gottpoff") &&
498 strcmp(name, "..tlsie"))
499 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
500 return;
503 if (is_global == 3) {
504 struct elf_symbol **s;
506 * Fix up a forward-reference symbol size from the first
507 * pass.
509 for (s = &fwds; *s; s = &(*s)->nextfwd)
510 if (!strcmp((*s)->name, name)) {
511 struct tokenval tokval;
512 expr *e;
513 char *p = nasm_skip_spaces(nasm_skip_word(special));
515 stdscan_reset();
516 stdscan_set(p);
517 tokval.t_type = TOKEN_INVALID;
518 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
519 if (e) {
520 if (!is_simple(e))
521 nasm_error(ERR_NONFATAL, "cannot use relocatable"
522 " expression as symbol size");
523 else
524 (*s)->size = reloc_value(e);
528 * Remove it from the list of unresolved sizes.
530 nasm_free((*s)->name);
531 *s = (*s)->nextfwd;
532 return;
534 return; /* it wasn't an important one */
537 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
538 strslen += 1 + strlen(name);
540 lastsym = sym = saa_wstruct(syms);
542 memset(&sym->symv, 0, sizeof(struct rbtree));
544 sym->strpos = pos;
545 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
546 sym->other = STV_DEFAULT;
547 sym->size = 0;
548 if (segment == NO_SEG)
549 sym->section = SHN_ABS;
550 else {
551 int i;
552 sym->section = SHN_UNDEF;
553 if (segment == def_seg) {
554 /* we have to be sure at least text section is there */
555 int tempint;
556 if (segment != elf_section_names(".text", 2, &tempint))
557 nasm_panic(0, "strange segment conditions in ELF driver");
559 for (i = 0; i < nsects; i++) {
560 if (segment == sects[i]->index) {
561 sym->section = i + 1;
562 break;
567 if (is_global == 2) {
568 sym->size = offset;
569 sym->symv.key = 0;
570 sym->section = SHN_COMMON;
572 * We have a common variable. Check the special text to see
573 * if it's a valid number and power of two; if so, store it
574 * as the alignment for the common variable.
576 if (special) {
577 bool err;
578 sym->symv.key = readnum(special, &err);
579 if (err)
580 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
581 " valid number", special);
582 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
583 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
584 " power of two", special);
586 special_used = true;
587 } else
588 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
590 if (sym->type == SYM_GLOBAL) {
592 * If sym->section == SHN_ABS, then the first line of the
593 * else section would cause a core dump, because its a reference
594 * beyond the end of the section array.
595 * This behaviour is exhibited by this code:
596 * GLOBAL crash_nasm
597 * crash_nasm equ 0
598 * To avoid such a crash, such requests are silently discarded.
599 * This may not be the best solution.
601 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
602 bsym = raa_write(bsym, segment, nglobs);
603 } else if (sym->section != SHN_ABS) {
605 * This is a global symbol; so we must add it to the rbtree
606 * of global symbols in its section.
608 * In addition, we check the special text for symbol
609 * type and size information.
611 sects[sym->section-1]->gsyms =
612 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
614 if (special) {
615 int n = strcspn(special, " \t");
617 if (!nasm_strnicmp(special, "function", n))
618 sym->type |= STT_FUNC;
619 else if (!nasm_strnicmp(special, "data", n) ||
620 !nasm_strnicmp(special, "object", n))
621 sym->type |= STT_OBJECT;
622 else if (!nasm_strnicmp(special, "notype", n))
623 sym->type |= STT_NOTYPE;
624 else
625 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
626 n, special);
627 special += n;
629 special = nasm_skip_spaces(special);
630 if (*special) {
631 n = strcspn(special, " \t");
632 if (!nasm_strnicmp(special, "default", n))
633 sym->other = STV_DEFAULT;
634 else if (!nasm_strnicmp(special, "internal", n))
635 sym->other = STV_INTERNAL;
636 else if (!nasm_strnicmp(special, "hidden", n))
637 sym->other = STV_HIDDEN;
638 else if (!nasm_strnicmp(special, "protected", n))
639 sym->other = STV_PROTECTED;
640 else
641 n = 0;
642 special += n;
645 if (*special) {
646 struct tokenval tokval;
647 expr *e;
648 int fwd = 0;
649 char *saveme = stdscan_get();
651 while (special[n] && nasm_isspace(special[n]))
652 n++;
654 * We have a size expression; attempt to
655 * evaluate it.
657 stdscan_reset();
658 stdscan_set(special + n);
659 tokval.t_type = TOKEN_INVALID;
660 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
661 if (fwd) {
662 sym->nextfwd = fwds;
663 fwds = sym;
664 sym->name = nasm_strdup(name);
665 } else if (e) {
666 if (!is_simple(e))
667 nasm_error(ERR_NONFATAL, "cannot use relocatable"
668 " expression as symbol size");
669 else
670 sym->size = reloc_value(e);
672 stdscan_set(saveme);
674 special_used = true;
677 * If TLS segment, mark symbol accordingly.
679 if (sects[sym->section - 1]->flags & SHF_TLS) {
680 sym->type &= 0xf0;
681 sym->type |= STT_TLS;
684 sym->globnum = nglobs;
685 nglobs++;
686 } else
687 nlocals++;
689 if (special && !special_used)
690 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
693 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
694 int64_t offset, int type)
696 struct elf_reloc *r;
698 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
699 sect->tail = &r->next;
701 r->address = sect->len;
702 r->offset = offset;
704 if (segment != NO_SEG) {
705 int i;
706 for (i = 0; i < nsects; i++)
707 if (segment == sects[i]->index)
708 r->symbol = i + 2;
709 if (!r->symbol)
710 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
712 r->type = type;
714 sect->nrelocs++;
718 * This routine deals with ..got and ..sym relocations: the more
719 * complicated kinds. In shared-library writing, some relocations
720 * with respect to global symbols must refer to the precise symbol
721 * rather than referring to an offset from the base of the section
722 * _containing_ the symbol. Such relocations call to this routine,
723 * which searches the symbol list for the symbol in question.
725 * R_386_GOT32 | R_X86_64_GOT32 references require the _exact_ symbol address to be
726 * used; R_386_32 | R_X86_64_32 references can be at an offset from the symbol.
727 * The boolean argument `exact' tells us this.
729 * Return value is the adjusted value of `addr', having become an
730 * offset from the symbol rather than the section. Should always be
731 * zero when returning from an exact call.
733 * Limitation: if you define two symbols at the same place,
734 * confusion will occur.
736 * Inefficiency: we search, currently, using a linked list which
737 * isn't even necessarily sorted.
739 static int64_t elf_add_gsym_reloc(struct elf_section *sect,
740 int32_t segment, uint64_t offset,
741 int64_t pcrel, int type, bool exact)
743 struct elf_reloc *r;
744 struct elf_section *s;
745 struct elf_symbol *sym;
746 struct rbtree *srb;
747 int i;
750 * First look up the segment/offset pair and find a global
751 * symbol corresponding to it. If it's not one of our segments,
752 * then it must be an external symbol, in which case we're fine
753 * doing a normal elf_add_reloc after first sanity-checking
754 * that the offset from the symbol is zero.
756 s = NULL;
757 for (i = 0; i < nsects; i++)
758 if (segment == sects[i]->index) {
759 s = sects[i];
760 break;
763 if (!s) {
764 if (exact && offset)
765 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
766 else
767 elf_add_reloc(sect, segment, offset - pcrel, type);
768 return 0;
771 srb = rb_search(s->gsyms, offset);
772 if (!srb || (exact && srb->key != offset)) {
773 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
774 " for this reference");
775 return 0;
777 sym = container_of(srb, struct elf_symbol, symv);
779 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
780 sect->tail = &r->next;
782 r->next = NULL;
783 r->address = sect->len;
784 r->offset = offset - pcrel - sym->symv.key;
785 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
786 r->type = type;
788 sect->nrelocs++;
789 return r->offset;
792 static void elf32_out(int32_t segto, const void *data,
793 enum out_type type, uint64_t size,
794 int32_t segment, int32_t wrt)
796 struct elf_section *s;
797 int64_t addr;
798 int reltype, bytes;
799 int i;
800 static struct symlininfo sinfo;
802 s = NULL;
803 for (i = 0; i < nsects; i++)
804 if (segto == sects[i]->index) {
805 s = sects[i];
806 break;
808 if (!s) {
809 int tempint; /* ignored */
810 if (segto != elf_section_names(".text", 2, &tempint))
811 nasm_panic(0, "strange segment conditions in ELF driver");
812 else {
813 s = sects[nsects - 1];
814 i = nsects - 1;
818 /* again some stabs debugging stuff */
819 sinfo.offset = s->len;
820 sinfo.section = i;
821 sinfo.segto = segto;
822 sinfo.name = s->name;
823 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
824 /* end of debugging stuff */
826 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
827 nasm_error(ERR_WARNING, "attempt to initialize memory in"
828 " BSS section `%s': ignored", s->name);
829 s->len += realsize(type, size);
830 return;
833 switch (type) {
834 case OUT_RESERVE:
835 if (s->type == SHT_PROGBITS) {
836 nasm_error(ERR_WARNING, "uninitialized space declared in"
837 " non-BSS section `%s': zeroing", s->name);
838 elf_sect_write(s, NULL, size);
839 } else
840 s->len += size;
841 break;
843 case OUT_RAWDATA:
844 elf_sect_write(s, data, size);
845 break;
847 case OUT_ADDRESS:
849 bool gnu16 = false;
850 int asize = abs((int)size);
852 addr = *(int64_t *)data;
853 if (segment != NO_SEG) {
854 if (segment % 2) {
855 nasm_error(ERR_NONFATAL, "ELF format does not support"
856 " segment base references");
857 } else {
858 if (wrt == NO_SEG) {
860 * The if() is a hack to deal with compilers which
861 * don't handle switch() statements with 64-bit
862 * expressions.
864 switch (asize) {
865 case 1:
866 gnu16 = true;
867 elf_add_reloc(s, segment, 0, R_386_8);
868 break;
869 case 2:
870 gnu16 = true;
871 elf_add_reloc(s, segment, 0, R_386_16);
872 break;
873 case 4:
874 elf_add_reloc(s, segment, 0, R_386_32);
875 break;
876 default: /* Error issued further down */
877 break;
879 } else if (wrt == elf_gotpc_sect + 1) {
881 * The user will supply GOT relative to $$. ELF
882 * will let us have GOT relative to $. So we
883 * need to fix up the data item by $-$$.
885 addr += s->len;
886 elf_add_reloc(s, segment, 0, R_386_GOTPC);
887 } else if (wrt == elf_gotoff_sect + 1) {
888 elf_add_reloc(s, segment, 0, R_386_GOTOFF);
889 } else if (wrt == elf_tlsie_sect + 1) {
890 addr = elf_add_gsym_reloc(s, segment, addr, 0,
891 R_386_TLS_IE, true);
892 } else if (wrt == elf_got_sect + 1) {
893 addr = elf_add_gsym_reloc(s, segment, addr, 0,
894 R_386_GOT32, true);
895 } else if (wrt == elf_sym_sect + 1) {
896 switch (asize) {
897 case 1:
898 gnu16 = true;
899 addr = elf_add_gsym_reloc(s, segment, addr, 0,
900 R_386_8, false);
901 break;
902 case 2:
903 gnu16 = true;
904 addr = elf_add_gsym_reloc(s, segment, addr, 0,
905 R_386_16, false);
906 break;
907 case 4:
908 addr = elf_add_gsym_reloc(s, segment, addr, 0,
909 R_386_32, false);
910 break;
911 default:
912 break;
914 } else if (wrt == elf_plt_sect + 1) {
915 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
916 "relative PLT references");
917 } else {
918 nasm_error(ERR_NONFATAL, "ELF format does not support this"
919 " use of WRT");
920 wrt = NO_SEG; /* we can at least _try_ to continue */
925 if (gnu16) {
926 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
927 "8- or 16-bit relocations in ELF32 is a GNU extension");
928 } else if (asize != 4 && segment != NO_SEG) {
929 nasm_error(ERR_NONFATAL, "Unsupported non-32-bit ELF relocation");
931 elf_sect_writeaddr(s, addr, asize);
932 break;
935 case OUT_REL1ADR:
936 reltype = R_386_PC8;
937 bytes = 1;
938 goto rel12adr;
939 case OUT_REL2ADR:
940 reltype = R_386_PC16;
941 bytes = 2;
942 goto rel12adr;
944 rel12adr:
945 addr = *(int64_t *)data - size;
946 nasm_assert(segment != segto);
947 if (segment != NO_SEG && segment % 2) {
948 nasm_error(ERR_NONFATAL, "ELF format does not support"
949 " segment base references");
950 } else {
951 if (wrt == NO_SEG) {
952 nasm_error(ERR_WARNING | ERR_WARN_GNUELF,
953 "8- or 16-bit relocations in ELF is a GNU extension");
954 elf_add_reloc(s, segment, 0, reltype);
955 } else {
956 nasm_error(ERR_NONFATAL,
957 "Unsupported non-32-bit ELF relocation");
960 elf_sect_writeaddr(s, addr, bytes);
961 break;
963 case OUT_REL4ADR:
964 addr = *(int64_t *)data - size;
965 if (segment == segto)
966 nasm_panic(0, "intra-segment OUT_REL4ADR");
967 if (segment != NO_SEG && segment % 2) {
968 nasm_error(ERR_NONFATAL, "ELF format does not support"
969 " segment base references");
970 } else {
971 if (wrt == NO_SEG) {
972 elf_add_reloc(s, segment, 0, R_386_PC32);
973 } else if (wrt == elf_plt_sect + 1) {
974 elf_add_reloc(s, segment, 0, R_386_PLT32);
975 } else if (wrt == elf_gotpc_sect + 1 ||
976 wrt == elf_gotoff_sect + 1 ||
977 wrt == elf_got_sect + 1) {
978 nasm_error(ERR_NONFATAL, "ELF format cannot produce PC-"
979 "relative GOT references");
980 } else {
981 nasm_error(ERR_NONFATAL, "ELF format does not support this"
982 " use of WRT");
983 wrt = NO_SEG; /* we can at least _try_ to continue */
986 elf_sect_writeaddr(s, addr, 4);
987 break;
989 case OUT_REL8ADR:
990 nasm_error(ERR_NONFATAL, "32-bit ELF format does not support 64-bit relocations");
991 addr = 0;
992 elf_sect_writeaddr(s, addr, 8);
993 break;
995 default:
996 panic();
999 static void elf64_out(int32_t segto, const void *data,
1000 enum out_type type, uint64_t size,
1001 int32_t segment, int32_t wrt)
1003 struct elf_section *s;
1004 int64_t addr;
1005 int reltype, bytes;
1006 int i;
1007 static struct symlininfo sinfo;
1010 * handle absolute-assembly (structure definitions)
1012 if (segto == NO_SEG) {
1013 if (type != OUT_RESERVE)
1014 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1015 " space");
1016 return;
1019 s = NULL;
1020 for (i = 0; i < nsects; i++)
1021 if (segto == sects[i]->index) {
1022 s = sects[i];
1023 break;
1025 if (!s) {
1026 int tempint; /* ignored */
1027 if (segto != elf_section_names(".text", 2, &tempint))
1028 nasm_panic(0, "strange segment conditions in ELF driver");
1029 else {
1030 s = sects[nsects - 1];
1031 i = nsects - 1;
1035 /* again some stabs debugging stuff */
1036 sinfo.offset = s->len;
1037 sinfo.section = i;
1038 sinfo.segto = segto;
1039 sinfo.name = s->name;
1040 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1041 /* end of debugging stuff */
1043 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1044 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1045 " BSS section `%s': ignored", s->name);
1046 s->len += realsize(type, size);
1047 return;
1050 switch (type) {
1051 case OUT_RESERVE:
1052 if (s->type == SHT_PROGBITS) {
1053 nasm_error(ERR_WARNING, "uninitialized space declared in"
1054 " non-BSS section `%s': zeroing", s->name);
1055 elf_sect_write(s, NULL, size);
1056 } else
1057 s->len += size;
1058 break;
1060 case OUT_RAWDATA:
1061 if (segment != NO_SEG)
1062 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1063 elf_sect_write(s, data, size);
1064 break;
1066 case OUT_ADDRESS:
1068 int isize = (int)size;
1069 int asize = abs((int)size);
1071 addr = *(int64_t *)data;
1072 if (segment == NO_SEG) {
1073 /* Do nothing */
1074 } else if (segment % 2) {
1075 nasm_error(ERR_NONFATAL, "ELF format does not support"
1076 " segment base references");
1077 } else {
1078 if (wrt == NO_SEG) {
1079 switch (isize) {
1080 case 1:
1081 case -1:
1082 elf_add_reloc(s, segment, addr, R_X86_64_8);
1083 break;
1084 case 2:
1085 case -2:
1086 elf_add_reloc(s, segment, addr, R_X86_64_16);
1087 break;
1088 case 4:
1089 elf_add_reloc(s, segment, addr, R_X86_64_32);
1090 break;
1091 case -4:
1092 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1093 break;
1094 case 8:
1095 case -8:
1096 elf_add_reloc(s, segment, addr, R_X86_64_64);
1097 break;
1098 default:
1099 nasm_panic(0, "internal error elf64-hpa-871");
1100 break;
1102 addr = 0;
1103 } else if (wrt == elf_gotpc_sect + 1) {
1105 * The user will supply GOT relative to $$. ELF
1106 * will let us have GOT relative to $. So we
1107 * need to fix up the data item by $-$$.
1109 addr += s->len;
1110 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1111 addr = 0;
1112 } else if (wrt == elf_gotoff_sect + 1) {
1113 if (asize != 8) {
1114 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
1115 "references to be qword");
1116 } else {
1117 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
1118 addr = 0;
1120 } else if (wrt == elf_got_sect + 1) {
1121 switch (asize) {
1122 case 4:
1123 elf_add_gsym_reloc(s, segment, addr, 0,
1124 R_X86_64_GOT32, true);
1125 addr = 0;
1126 break;
1127 case 8:
1128 elf_add_gsym_reloc(s, segment, addr, 0,
1129 R_X86_64_GOT64, true);
1130 addr = 0;
1131 break;
1132 default:
1133 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1134 break;
1136 } else if (wrt == elf_sym_sect + 1) {
1137 switch (isize) {
1138 case 1:
1139 case -1:
1140 elf_add_gsym_reloc(s, segment, addr, 0,
1141 R_X86_64_8, false);
1142 addr = 0;
1143 break;
1144 case 2:
1145 case -2:
1146 elf_add_gsym_reloc(s, segment, addr, 0,
1147 R_X86_64_16, false);
1148 addr = 0;
1149 break;
1150 case 4:
1151 elf_add_gsym_reloc(s, segment, addr, 0,
1152 R_X86_64_32, false);
1153 addr = 0;
1154 break;
1155 case -4:
1156 elf_add_gsym_reloc(s, segment, addr, 0,
1157 R_X86_64_32S, false);
1158 addr = 0;
1159 break;
1160 case 8:
1161 case -8:
1162 elf_add_gsym_reloc(s, segment, addr, 0,
1163 R_X86_64_64, false);
1164 addr = 0;
1165 break;
1166 default:
1167 nasm_panic(0, "internal error elf64-hpa-903");
1168 break;
1170 } else if (wrt == elf_plt_sect + 1) {
1171 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1172 "relative PLT references");
1173 } else {
1174 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1175 " use of WRT");
1178 elf_sect_writeaddr(s, addr, asize);
1179 break;
1182 case OUT_REL1ADR:
1183 reltype = R_X86_64_PC8;
1184 bytes = 1;
1185 goto rel12adr;
1187 case OUT_REL2ADR:
1188 reltype = R_X86_64_PC16;
1189 bytes = 2;
1190 goto rel12adr;
1192 rel12adr:
1193 addr = *(int64_t *)data - size;
1194 if (segment == segto)
1195 nasm_panic(0, "intra-segment OUT_REL1ADR");
1196 if (segment == NO_SEG) {
1197 /* Do nothing */
1198 } else if (segment % 2) {
1199 nasm_error(ERR_NONFATAL, "ELF format does not support"
1200 " segment base references");
1201 } else {
1202 if (wrt == NO_SEG) {
1203 elf_add_reloc(s, segment, addr, reltype);
1204 addr = 0;
1205 } else {
1206 nasm_error(ERR_NONFATAL,
1207 "Unsupported non-32-bit ELF relocation");
1210 elf_sect_writeaddr(s, addr, bytes);
1211 break;
1213 case OUT_REL4ADR:
1214 addr = *(int64_t *)data - size;
1215 if (segment == segto)
1216 nasm_panic(0, "intra-segment OUT_REL4ADR");
1217 if (segment == NO_SEG) {
1218 /* Do nothing */
1219 } else if (segment % 2) {
1220 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1221 " segment base references");
1222 } else {
1223 if (wrt == NO_SEG) {
1224 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1225 addr = 0;
1226 } else if (wrt == elf_plt_sect + 1) {
1227 elf_add_gsym_reloc(s, segment, addr+size, size,
1228 R_X86_64_PLT32, true);
1229 addr = 0;
1230 } else if (wrt == elf_gotpc_sect + 1 ||
1231 wrt == elf_got_sect + 1) {
1232 elf_add_gsym_reloc(s, segment, addr+size, size,
1233 R_X86_64_GOTPCREL, true);
1234 addr = 0;
1235 } else if (wrt == elf_gotoff_sect + 1 ||
1236 wrt == elf_got_sect + 1) {
1237 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1238 "qword absolute");
1239 } else if (wrt == elf_gottpoff_sect + 1) {
1240 elf_add_gsym_reloc(s, segment, addr+size, size,
1241 R_X86_64_GOTTPOFF, true);
1242 addr = 0;
1243 } else {
1244 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1245 " use of WRT");
1248 elf_sect_writeaddr(s, addr, 4);
1249 break;
1251 case OUT_REL8ADR:
1252 addr = *(int64_t *)data - size;
1253 if (segment == segto)
1254 nasm_panic(0, "intra-segment OUT_REL8ADR");
1255 if (segment == NO_SEG) {
1256 /* Do nothing */
1257 } else if (segment % 2) {
1258 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
1259 " segment base references");
1260 } else {
1261 if (wrt == NO_SEG) {
1262 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
1263 addr = 0;
1264 } else if (wrt == elf_gotpc_sect + 1 ||
1265 wrt == elf_got_sect + 1) {
1266 elf_add_gsym_reloc(s, segment, addr+size, size,
1267 R_X86_64_GOTPCREL64, true);
1268 addr = 0;
1269 } else if (wrt == elf_gotoff_sect + 1 ||
1270 wrt == elf_got_sect + 1) {
1271 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
1272 "absolute");
1273 } else if (wrt == elf_gottpoff_sect + 1) {
1274 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
1275 "dword");
1276 } else {
1277 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
1278 " use of WRT");
1281 elf_sect_writeaddr(s, addr, 8);
1282 break;
1284 default:
1285 panic();
1289 static void elfx32_out(int32_t segto, const void *data,
1290 enum out_type type, uint64_t size,
1291 int32_t segment, int32_t wrt)
1293 struct elf_section *s;
1294 int64_t addr;
1295 int reltype, bytes;
1296 int i;
1297 static struct symlininfo sinfo;
1300 * handle absolute-assembly (structure definitions)
1302 if (segto == NO_SEG) {
1303 if (type != OUT_RESERVE)
1304 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
1305 " space");
1306 return;
1309 s = NULL;
1310 for (i = 0; i < nsects; i++)
1311 if (segto == sects[i]->index) {
1312 s = sects[i];
1313 break;
1315 if (!s) {
1316 int tempint; /* ignored */
1317 if (segto != elf_section_names(".text", 2, &tempint))
1318 nasm_panic(0, "strange segment conditions in ELF driver");
1319 else {
1320 s = sects[nsects - 1];
1321 i = nsects - 1;
1325 /* again some stabs debugging stuff */
1326 sinfo.offset = s->len;
1327 sinfo.section = i;
1328 sinfo.segto = segto;
1329 sinfo.name = s->name;
1330 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
1331 /* end of debugging stuff */
1333 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
1334 nasm_error(ERR_WARNING, "attempt to initialize memory in"
1335 " BSS section `%s': ignored", s->name);
1336 s->len += realsize(type, size);
1337 return;
1340 switch (type) {
1341 case OUT_RESERVE:
1342 if (s->type == SHT_PROGBITS) {
1343 nasm_error(ERR_WARNING, "uninitialized space declared in"
1344 " non-BSS section `%s': zeroing", s->name);
1345 elf_sect_write(s, NULL, size);
1346 } else
1347 s->len += size;
1348 break;
1350 case OUT_RAWDATA:
1351 if (segment != NO_SEG)
1352 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
1353 elf_sect_write(s, data, size);
1354 break;
1356 case OUT_ADDRESS:
1358 int isize = (int)size;
1359 int asize = abs((int)size);
1361 addr = *(int64_t *)data;
1362 if (segment == NO_SEG) {
1363 /* Do nothing */
1364 } else if (segment % 2) {
1365 nasm_error(ERR_NONFATAL, "ELF format does not support"
1366 " segment base references");
1367 } else {
1368 if (wrt == NO_SEG) {
1369 switch (isize) {
1370 case 1:
1371 case -1:
1372 elf_add_reloc(s, segment, addr, R_X86_64_8);
1373 break;
1374 case 2:
1375 case -2:
1376 elf_add_reloc(s, segment, addr, R_X86_64_16);
1377 break;
1378 case 4:
1379 elf_add_reloc(s, segment, addr, R_X86_64_32);
1380 break;
1381 case -4:
1382 elf_add_reloc(s, segment, addr, R_X86_64_32S);
1383 break;
1384 case 8:
1385 case -8:
1386 elf_add_reloc(s, segment, addr, R_X86_64_64);
1387 break;
1388 default:
1389 nasm_panic(0, "internal error elfx32-hpa-871");
1390 break;
1392 addr = 0;
1393 } else if (wrt == elf_gotpc_sect + 1) {
1395 * The user will supply GOT relative to $$. ELF
1396 * will let us have GOT relative to $. So we
1397 * need to fix up the data item by $-$$.
1399 addr += s->len;
1400 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
1401 addr = 0;
1402 } else if (wrt == elf_gotoff_sect + 1) {
1403 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
1404 "R_X86_64_GOTOFF64");
1405 } else if (wrt == elf_got_sect + 1) {
1406 switch (asize) {
1407 case 4:
1408 elf_add_gsym_reloc(s, segment, addr, 0,
1409 R_X86_64_GOT32, true);
1410 addr = 0;
1411 break;
1412 default:
1413 nasm_error(ERR_NONFATAL, "invalid ..got reference");
1414 break;
1416 } else if (wrt == elf_sym_sect + 1) {
1417 switch (isize) {
1418 case 1:
1419 case -1:
1420 elf_add_gsym_reloc(s, segment, addr, 0,
1421 R_X86_64_8, false);
1422 addr = 0;
1423 break;
1424 case 2:
1425 case -2:
1426 elf_add_gsym_reloc(s, segment, addr, 0,
1427 R_X86_64_16, false);
1428 addr = 0;
1429 break;
1430 case 4:
1431 elf_add_gsym_reloc(s, segment, addr, 0,
1432 R_X86_64_32, false);
1433 addr = 0;
1434 break;
1435 case -4:
1436 elf_add_gsym_reloc(s, segment, addr, 0,
1437 R_X86_64_32S, false);
1438 addr = 0;
1439 break;
1440 case 8:
1441 case -8:
1442 elf_add_gsym_reloc(s, segment, addr, 0,
1443 R_X86_64_64, false);
1444 addr = 0;
1445 break;
1446 default:
1447 nasm_panic(0, "internal error elfx32-hpa-903");
1448 break;
1450 } else if (wrt == elf_plt_sect + 1) {
1451 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1452 "relative PLT references");
1453 } else {
1454 nasm_error(ERR_NONFATAL, "ELF format does not support this"
1455 " use of WRT");
1458 elf_sect_writeaddr(s, addr, asize);
1459 break;
1462 case OUT_REL1ADR:
1463 reltype = R_X86_64_PC8;
1464 bytes = 1;
1465 goto rel12adr;
1467 case OUT_REL2ADR:
1468 reltype = R_X86_64_PC16;
1469 bytes = 2;
1470 goto rel12adr;
1472 rel12adr:
1473 addr = *(int64_t *)data - size;
1474 if (segment == segto)
1475 nasm_panic(0, "intra-segment OUT_REL1ADR");
1476 if (segment == NO_SEG) {
1477 /* Do nothing */
1478 } else if (segment % 2) {
1479 nasm_error(ERR_NONFATAL, "ELF format does not support"
1480 " segment base references");
1481 } else {
1482 if (wrt == NO_SEG) {
1483 elf_add_reloc(s, segment, addr, reltype);
1484 addr = 0;
1485 } else {
1486 nasm_error(ERR_NONFATAL,
1487 "Unsupported non-32-bit ELF relocation");
1490 elf_sect_writeaddr(s, addr, bytes);
1491 break;
1493 case OUT_REL4ADR:
1494 addr = *(int64_t *)data - size;
1495 if (segment == segto)
1496 nasm_panic(0, "intra-segment OUT_REL4ADR");
1497 if (segment == NO_SEG) {
1498 /* Do nothing */
1499 } else if (segment % 2) {
1500 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
1501 " segment base references");
1502 } else {
1503 if (wrt == NO_SEG) {
1504 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
1505 addr = 0;
1506 } else if (wrt == elf_plt_sect + 1) {
1507 elf_add_gsym_reloc(s, segment, addr+size, size,
1508 R_X86_64_PLT32, true);
1509 addr = 0;
1510 } else if (wrt == elf_gotpc_sect + 1 ||
1511 wrt == elf_got_sect + 1) {
1512 elf_add_gsym_reloc(s, segment, addr+size, size,
1513 R_X86_64_GOTPCREL, true);
1514 addr = 0;
1515 } else if (wrt == elf_gotoff_sect + 1 ||
1516 wrt == elf_got_sect + 1) {
1517 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
1518 } else if (wrt == elf_gottpoff_sect + 1) {
1519 elf_add_gsym_reloc(s, segment, addr+size, size,
1520 R_X86_64_GOTTPOFF, true);
1521 addr = 0;
1522 } else {
1523 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
1524 " use of WRT");
1527 elf_sect_writeaddr(s, addr, 4);
1528 break;
1530 case OUT_REL8ADR:
1531 nasm_error(ERR_NONFATAL, "32-bit ELF format does not support 64-bit relocations");
1532 addr = 0;
1533 elf_sect_writeaddr(s, addr, 8);
1534 break;
1536 default:
1537 panic();
1541 static void elf_write(void)
1543 int align;
1544 char *p;
1545 int i;
1547 struct SAA *symtab;
1548 int32_t symtablen, symtablocal;
1551 * Work out how many sections we will have. We have SHN_UNDEF,
1552 * then the flexible user sections, then the fixed sections
1553 * `.shstrtab', `.symtab' and `.strtab', then optionally
1554 * relocation sections for the user sections.
1556 nsections = sec_numspecial + 1;
1557 if (dfmt_is_stabs())
1558 nsections += 3;
1559 else if (dfmt_is_dwarf())
1560 nsections += 10;
1562 add_sectname("", ".shstrtab");
1563 add_sectname("", ".symtab");
1564 add_sectname("", ".strtab");
1565 for (i = 0; i < nsects; i++) {
1566 nsections++; /* for the section itself */
1567 if (sects[i]->head) {
1568 nsections++; /* for its relocations */
1569 add_sectname(is_elf32() ? ".rel" : ".rela", sects[i]->name);
1573 if (dfmt_is_stabs()) {
1574 /* in case the debug information is wanted, just add these three sections... */
1575 add_sectname("", ".stab");
1576 add_sectname("", ".stabstr");
1577 add_sectname(is_elf32() ? ".rel" : ".rela", ".stab");
1578 } else if (dfmt_is_dwarf()) {
1579 /* the dwarf debug standard specifies the following ten sections,
1580 not all of which are currently implemented,
1581 although all of them are defined. */
1582 #define debug_aranges (int64_t) (nsections-10)
1583 #define debug_info (int64_t) (nsections-7)
1584 #define debug_abbrev (int64_t) (nsections-5)
1585 #define debug_line (int64_t) (nsections-4)
1586 add_sectname("", ".debug_aranges");
1587 add_sectname(".rela", ".debug_aranges");
1588 add_sectname("", ".debug_pubnames");
1589 add_sectname("", ".debug_info");
1590 add_sectname(".rela", ".debug_info");
1591 add_sectname("", ".debug_abbrev");
1592 add_sectname("", ".debug_line");
1593 add_sectname(".rela", ".debug_line");
1594 add_sectname("", ".debug_frame");
1595 add_sectname("", ".debug_loc");
1599 * Output the ELF header.
1601 if (is_elf32() || is_elfx32()) {
1602 Elf32_Ehdr ehdr;
1604 nasm_zero(ehdr.e_ident);
1605 memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
1606 ehdr.e_ident[EI_CLASS] = ELFCLASS32;
1607 ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
1608 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1609 ehdr.e_ident[EI_OSABI] = elf_osabi;
1610 ehdr.e_ident[EI_ABIVERSION] = elf_abiver;
1612 ehdr.e_type = cpu_to_le16(ET_REL);
1613 ehdr.e_machine = cpu_to_le16(is_elf32() ? EM_386 : EM_X86_64);
1614 ehdr.e_version = cpu_to_le16(EV_CURRENT);
1615 ehdr.e_entry = 0;
1616 ehdr.e_phoff = 0;
1617 ehdr.e_shoff = sizeof(Elf64_Ehdr);
1618 ehdr.e_flags = 0;
1619 ehdr.e_ehsize = cpu_to_le16(sizeof(Elf32_Ehdr));
1620 ehdr.e_phentsize = 0;
1621 ehdr.e_phnum = 0;
1622 ehdr.e_shentsize = cpu_to_le16(sizeof(Elf32_Shdr));
1623 ehdr.e_shnum = cpu_to_le16(nsections);
1624 ehdr.e_shstrndx = cpu_to_le16(sec_shstrtab);
1626 nasm_write(&ehdr, sizeof(ehdr), ofile);
1627 fwritezero(sizeof(Elf64_Ehdr) - sizeof(Elf32_Ehdr), ofile);
1628 } else {
1629 Elf64_Ehdr ehdr;
1631 nasm_assert(is_elf64());
1633 nasm_zero(ehdr.e_ident);
1634 memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
1635 ehdr.e_ident[EI_CLASS] = ELFCLASS64;
1636 ehdr.e_ident[EI_DATA] = ELFDATA2LSB;
1637 ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1638 ehdr.e_ident[EI_OSABI] = elf_osabi;
1639 ehdr.e_ident[EI_ABIVERSION] = elf_abiver;
1641 ehdr.e_type = cpu_to_le16(ET_REL);
1642 ehdr.e_machine = cpu_to_le16(EM_X86_64);
1643 ehdr.e_version = cpu_to_le16(EV_CURRENT);
1644 ehdr.e_entry = 0;
1645 ehdr.e_phoff = 0;
1646 ehdr.e_shoff = sizeof(Elf64_Ehdr);
1647 ehdr.e_flags = 0;
1648 ehdr.e_ehsize = cpu_to_le16(sizeof(Elf64_Ehdr));
1649 ehdr.e_phentsize = 0;
1650 ehdr.e_phnum = 0;
1651 ehdr.e_shentsize = cpu_to_le16(sizeof(Elf64_Shdr));
1652 ehdr.e_shnum = cpu_to_le16(nsections);
1653 ehdr.e_shstrndx = cpu_to_le16(sec_shstrtab);
1655 nasm_write(&ehdr, sizeof(ehdr), ofile);
1659 * Build the symbol table and relocation tables.
1661 symtab = elf_build_symtab(&symtablen, &symtablocal);
1662 for (i = 0; i < nsects; i++)
1663 if (sects[i]->head)
1664 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1665 sects[i]->head);
1668 * Now output the section header table.
1671 elf_foffs = sizeof(Elf64_Ehdr) + (is_elf64() ? sizeof(Elf64_Shdr): sizeof(Elf32_Shdr)) * nsections;
1672 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1673 elf_foffs += align;
1674 elf_nsect = 0;
1675 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1677 /* SHN_UNDEF */
1678 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1679 p = shstrtab + 1;
1681 /* The normal sections */
1682 for (i = 0; i < nsects; i++) {
1683 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1684 (sects[i]->type == SHT_PROGBITS ?
1685 sects[i]->data : NULL), true,
1686 sects[i]->len, 0, 0, sects[i]->align, 0);
1687 p += strlen(p) + 1;
1690 /* .shstrtab */
1691 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1692 shstrtablen, 0, 0, 1, 0);
1693 p += strlen(p) + 1;
1695 /* .symtab */
1696 if (is_elf64())
1697 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1698 symtablen, sec_strtab, symtablocal, 8, 24);
1699 else
1700 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1701 symtablen, sec_strtab, symtablocal, 4, 16);
1702 p += strlen(p) + 1;
1704 /* .strtab */
1705 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1706 strslen, 0, 0, 1, 0);
1707 p += strlen(p) + 1;
1709 /* The relocation sections */
1710 if (is_elf32()) {
1711 for (i = 0; i < nsects; i++) {
1712 if (sects[i]->head) {
1713 elf_section_header(p - shstrtab, SHT_REL, 0, sects[i]->rel, true,
1714 sects[i]->rellen, sec_symtab, i + 1, 4, 8);
1715 p += strlen(p) + 1;
1718 } else if (is_elfx32()) {
1719 for (i = 0; i < nsects; i++) {
1720 if (sects[i]->head) {
1721 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1722 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1723 p += strlen(p) + 1;
1726 } else {
1727 nasm_assert(is_elf64());
1728 for (i = 0; i < nsects; i++) {
1729 if (sects[i]->head) {
1730 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1731 sects[i]->rellen, sec_symtab, i + 1, 8, 24);
1732 p += strlen(p) + 1;
1737 if (dfmt_is_stabs()) {
1738 /* for debugging information, create the last three sections
1739 which are the .stab , .stabstr and .rel.stab sections respectively */
1741 /* this function call creates the stab sections in memory */
1742 stabs_generate();
1744 if (stabbuf && stabstrbuf && stabrelbuf) {
1745 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1746 stablen, sec_stabstr, 0, 4, 12);
1747 p += strlen(p) + 1;
1749 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1750 stabstrlen, 0, 0, 4, 0);
1751 p += strlen(p) + 1;
1753 /* link -> symtable info -> section to refer to */
1754 if (is_elf32()) {
1755 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1756 stabrellen, sec_symtab, sec_stab, 4, 8);
1757 } else {
1758 elf_section_header(p - shstrtab, SHT_RELA, 0, stabrelbuf, false,
1759 stabrellen, sec_symtab, sec_stab, 4, is_elf64() ? 24 : 12);
1761 p += strlen(p) + 1;
1763 } else if (dfmt_is_dwarf()) {
1764 /* for dwarf debugging information, create the ten dwarf sections */
1766 /* this function call creates the dwarf sections in memory */
1767 if (dwarf_fsect)
1768 dwarf_generate();
1770 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1771 arangeslen, 0, 0, 1, 0);
1772 p += strlen(p) + 1;
1774 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1775 arangesrellen, sec_symtab,
1776 is_elf64() ? debug_aranges : sec_debug_aranges,
1777 1, is_elf64() ? 24 : 12);
1778 p += strlen(p) + 1;
1780 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf,
1781 false, pubnameslen, 0, 0, 1, 0);
1782 p += strlen(p) + 1;
1784 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1785 infolen, 0, 0, 1, 0);
1786 p += strlen(p) + 1;
1788 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1789 inforellen, sec_symtab,
1790 is_elf64() ? debug_info : sec_debug_info,
1791 1, is_elf64() ? 24 : 12);
1792 p += strlen(p) + 1;
1794 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1795 abbrevlen, 0, 0, 1, 0);
1796 p += strlen(p) + 1;
1798 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1799 linelen, 0, 0, 1, 0);
1800 p += strlen(p) + 1;
1802 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1803 linerellen, sec_symtab,
1804 is_elf64() ? debug_line : sec_debug_line,
1805 1, is_elf64() ? 24 : 12);
1806 p += strlen(p) + 1;
1808 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1809 framelen, 0, 0, 8, 0);
1810 p += strlen(p) + 1;
1812 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1813 loclen, 0, 0, 1, 0);
1814 p += strlen(p) + 1;
1816 fwritezero(align, ofile);
1819 * Now output the sections.
1821 elf_write_sections();
1823 nasm_free(elf_sects);
1824 saa_free(symtab);
1827 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1829 struct SAA *s = saa_init(1L);
1830 struct elf_symbol *sym;
1831 int i;
1833 size_t usize = is_elf64() ? sizeof(Elf64_Sym) : sizeof(Elf32_Sym);
1834 union {
1835 Elf32_Sym sym32;
1836 Elf64_Sym sym64;
1837 } u;
1839 *len = *local = 0;
1842 * Zero symbol first as required by spec.
1844 saa_wbytes(s, NULL, usize);
1845 *len += usize;
1846 (*local)++;
1849 * Next, an entry for the file name.
1851 if (is_elf64()) {
1852 u.sym64.st_name = cpu_to_le32(1);
1853 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_FILE);
1854 u.sym64.st_other = 0;
1855 u.sym64.st_shndx = cpu_to_le16(SHN_ABS);
1856 u.sym64.st_value = 0;
1857 u.sym64.st_size = 0;
1858 } else {
1859 u.sym32.st_name = cpu_to_le32(1);
1860 u.sym32.st_value = 0;
1861 u.sym32.st_size = 0;
1862 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_FILE);
1863 u.sym32.st_other = 0;
1864 u.sym32.st_shndx = cpu_to_le16(SHN_ABS);
1866 saa_wbytes(s, &u, usize);
1867 *len += usize;
1868 (*local)++;
1872 * Now some standard symbols defining the segments, for relocation
1873 * purposes.
1875 if (is_elf64()) {
1876 u.sym64.st_name = 0;
1877 u.sym64.st_other = 0;
1878 u.sym64.st_value = 0;
1879 u.sym64.st_size = 0;
1880 for (i = 1; i <= nsects; i++) {
1881 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_SECTION);
1882 u.sym64.st_shndx = cpu_to_le16(i);
1883 saa_wbytes(s, &u, usize);
1884 *len += usize;
1885 (*local)++;
1887 } else {
1888 u.sym32.st_name = 0;
1889 u.sym32.st_value = 0;
1890 u.sym32.st_size = 0;
1891 u.sym32.st_other = 0;
1892 for (i = 1; i <= nsects; i++) {
1893 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_SECTION);
1894 u.sym32.st_shndx = cpu_to_le16(i);
1895 saa_wbytes(s, &u, usize);
1896 *len += usize;
1897 (*local)++;
1902 * Now the other local symbols.
1904 saa_rewind(syms);
1905 if (is_elf64()) {
1906 while ((sym = saa_rstruct(syms))) {
1907 if (sym->type & SYM_GLOBAL)
1908 continue;
1909 u.sym64.st_name = cpu_to_le32(sym->strpos);
1910 u.sym64.st_info = sym->type;
1911 u.sym64.st_other = sym->other;
1912 u.sym64.st_shndx = cpu_to_le16(sym->section);
1913 u.sym64.st_value = cpu_to_le64(sym->symv.key);
1914 u.sym64.st_size = cpu_to_le64(sym->size);
1915 saa_wbytes(s, &u, usize);
1916 *len += usize;
1917 (*local)++;
1920 * dwarf needs symbols for debug sections
1921 * which are relocation targets.
1923 if (dfmt_is_dwarf()) {
1924 dwarf_infosym = *local;
1925 u.sym64.st_name = 0;
1926 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_SECTION);
1927 u.sym64.st_other = 0;
1928 u.sym64.st_shndx = cpu_to_le16(debug_info);
1929 u.sym64.st_value = 0;
1930 u.sym64.st_size = 0;
1931 saa_wbytes(s, &u, usize);
1932 *len += usize;
1933 (*local)++;
1934 dwarf_abbrevsym = *local;
1935 u.sym64.st_name = 0;
1936 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_SECTION);
1937 u.sym64.st_other = 0;
1938 u.sym64.st_shndx = cpu_to_le16(debug_abbrev);
1939 u.sym64.st_value = 0;
1940 u.sym64.st_size = 0;
1941 saa_wbytes(s, &u, usize);
1942 *len += usize;
1943 (*local)++;
1944 dwarf_linesym = *local;
1945 u.sym64.st_name = 0;
1946 u.sym64.st_info = ELF64_ST_INFO(STB_LOCAL, STT_SECTION);
1947 u.sym64.st_other = 0;
1948 u.sym64.st_shndx = cpu_to_le16(debug_line);
1949 u.sym64.st_value = 0;
1950 u.sym64.st_size = 0;
1951 saa_wbytes(s, &u, usize);
1952 *len += usize;
1953 (*local)++;
1955 } else {
1956 while ((sym = saa_rstruct(syms))) {
1957 if (sym->type & SYM_GLOBAL)
1958 continue;
1959 u.sym32.st_name = cpu_to_le32(sym->strpos);
1960 u.sym32.st_value = cpu_to_le32(sym->symv.key);
1961 u.sym32.st_size = cpu_to_le32(sym->size);
1962 u.sym32.st_info = sym->type;
1963 u.sym32.st_other = sym->other;
1964 u.sym32.st_shndx = cpu_to_le16(sym->section);
1965 saa_wbytes(s, &u, usize);
1966 *len += usize;
1967 (*local)++;
1970 * dwarf needs symbols for debug sections
1971 * which are relocation targets.
1973 if (dfmt_is_dwarf()) {
1974 dwarf_infosym = *local;
1975 u.sym32.st_name = 0;
1976 u.sym32.st_value = 0;
1977 u.sym32.st_size = 0;
1978 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_SECTION);
1979 u.sym32.st_other = 0;
1980 u.sym32.st_shndx = cpu_to_le16(sec_debug_info);
1981 saa_wbytes(s, &u, usize);
1982 *len += usize;
1983 (*local)++;
1984 dwarf_abbrevsym = *local;
1985 u.sym32.st_name = 0;
1986 u.sym32.st_value = 0;
1987 u.sym32.st_size = 0;
1988 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_SECTION);
1989 u.sym32.st_other = 0;
1990 u.sym32.st_shndx = cpu_to_le16(sec_debug_abbrev);
1991 saa_wbytes(s, &u, usize);
1992 *len += usize;
1993 (*local)++;
1994 dwarf_linesym = *local;
1995 u.sym32.st_name = 0;
1996 u.sym32.st_value = 0;
1997 u.sym32.st_size = 0;
1998 u.sym32.st_info = ELF32_ST_INFO(STB_LOCAL, STT_SECTION);
1999 u.sym32.st_other = 0;
2000 u.sym32.st_shndx = cpu_to_le16(sec_debug_line);
2001 saa_wbytes(s, &u, usize);
2002 *len += usize;
2003 (*local)++;
2008 * Now the global symbols.
2010 saa_rewind(syms);
2011 if (is_elf64()) {
2012 while ((sym = saa_rstruct(syms))) {
2013 if (!(sym->type & SYM_GLOBAL))
2014 continue;
2015 u.sym64.st_name = cpu_to_le32(sym->strpos);
2016 u.sym64.st_info = sym->type;
2017 u.sym64.st_other = sym->other;
2018 u.sym64.st_shndx = cpu_to_le16(sym->section);
2019 u.sym64.st_value = cpu_to_le64(sym->symv.key);
2020 u.sym64.st_size = cpu_to_le64(sym->size);
2021 saa_wbytes(s, &u, usize);
2022 *len += usize;
2024 } else {
2025 while ((sym = saa_rstruct(syms))) {
2026 if (!(sym->type & SYM_GLOBAL))
2027 continue;
2028 u.sym32.st_name = cpu_to_le32(sym->strpos);
2029 u.sym32.st_value = cpu_to_le32(sym->symv.key);
2030 u.sym32.st_size = cpu_to_le32(sym->size);
2031 u.sym32.st_info = sym->type;
2032 u.sym32.st_other = sym->other;
2033 u.sym32.st_shndx = cpu_to_le16(sym->section);
2034 saa_wbytes(s, &u, usize);
2035 *len += usize;
2039 return s;
2042 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
2044 struct SAA *s;
2045 int32_t global_offset;
2047 size_t usize = is_elf64() ? sizeof(Elf64_Rela) :
2048 (is_elfx32() ? sizeof(Elf32_Rela) : sizeof(Elf32_Rel));
2049 union {
2050 Elf32_Rel rel32;
2051 Elf32_Rela rela32;
2052 Elf64_Rela rela64;
2053 } u;
2055 if (!r)
2056 return NULL;
2058 s = saa_init(1L);
2059 *len = 0;
2062 * How to onvert from a global placeholder to a real symbol index;
2063 * the +2 refers to the two special entries, the null entry and
2064 * the filename entry.
2066 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
2068 if (is_elf32()) {
2069 while (r) {
2070 int32_t sym = r->symbol;
2072 if (sym >= GLOBAL_TEMP_BASE)
2073 sym += global_offset;
2075 u.rel32.r_offset = cpu_to_le32(r->address);
2076 u.rel32.r_info = cpu_to_le32(ELF32_R_INFO(sym, r->type));
2077 saa_wbytes(s, &u, usize);
2078 *len += usize;
2080 r = r->next;
2082 } else if (is_elfx32()) {
2083 while (r) {
2084 int32_t sym = r->symbol;
2086 if (sym >= GLOBAL_TEMP_BASE)
2087 sym += global_offset;
2089 u.rela32.r_offset = cpu_to_le32(r->address);
2090 u.rela32.r_info = cpu_to_le32(ELF32_R_INFO(sym, r->type));
2091 u.rela32.r_addend = cpu_to_le32(r->offset);
2092 saa_wbytes(s, &u, usize);
2093 *len += usize;
2095 r = r->next;
2097 } else {
2098 nasm_assert(is_elf64());
2099 while (r) {
2100 int32_t sym = r->symbol;
2102 if (sym >= GLOBAL_TEMP_BASE)
2103 sym += global_offset;
2105 u.rela64.r_offset = cpu_to_le64(r->address);
2106 u.rela64.r_info = cpu_to_le64(ELF64_R_INFO(sym, r->type));
2107 u.rela64.r_addend = cpu_to_le64(r->offset);
2108 saa_wbytes(s, &u, usize);
2109 *len += usize;
2111 r = r->next;
2115 return s;
2118 static void elf_section_header(int name, int type, uint64_t flags,
2119 void *data, bool is_saa, uint64_t datalen,
2120 int link, int info, int align, int eltsize)
2122 union {
2123 Elf32_Shdr shdr32;
2124 Elf64_Shdr shdr64;
2125 } shdr;
2127 elf_sects[elf_nsect].data = data;
2128 elf_sects[elf_nsect].len = datalen;
2129 elf_sects[elf_nsect].is_saa = is_saa;
2130 elf_nsect++;
2132 if (is_elf32() || is_elfx32()) {
2133 shdr.shdr32.sh_name = cpu_to_le32(name);
2134 shdr.shdr32.sh_type = cpu_to_le32(type);
2135 shdr.shdr32.sh_flags = cpu_to_le32(flags);
2136 shdr.shdr32.sh_addr = 0;
2137 shdr.shdr32.sh_offset = cpu_to_le32(type == SHT_NULL ? 0 : elf_foffs);
2138 shdr.shdr32.sh_size = cpu_to_le32(datalen);
2139 if (data)
2140 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2141 shdr.shdr32.sh_link = cpu_to_le32(link);
2142 shdr.shdr32.sh_info = cpu_to_le32(info);
2143 shdr.shdr32.sh_addralign = cpu_to_le32(align);
2144 shdr.shdr32.sh_entsize = cpu_to_le32(eltsize);
2145 } else {
2146 nasm_assert(is_elf64());
2148 shdr.shdr64.sh_name = cpu_to_le32(name);
2149 shdr.shdr64.sh_type = cpu_to_le32(type);
2150 shdr.shdr64.sh_flags = cpu_to_le64(flags);
2151 shdr.shdr64.sh_addr = 0;
2152 shdr.shdr64.sh_offset = cpu_to_le64(type == SHT_NULL ? 0 : elf_foffs);
2153 shdr.shdr64.sh_size = cpu_to_le32(datalen);
2154 if (data)
2155 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
2156 shdr.shdr64.sh_link = cpu_to_le32(link);
2157 shdr.shdr64.sh_info = cpu_to_le32(info);
2158 shdr.shdr64.sh_addralign = cpu_to_le64(align);
2159 shdr.shdr64.sh_entsize = cpu_to_le64(eltsize);
2162 nasm_write(&shdr, is_elf64() ? sizeof(shdr.shdr64) : sizeof(shdr.shdr32), ofile);
2165 static void elf_write_sections(void)
2167 int i;
2168 for (i = 0; i < elf_nsect; i++)
2169 if (elf_sects[i].data) {
2170 int32_t len = elf_sects[i].len;
2171 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
2172 int32_t align = reallen - len;
2173 if (elf_sects[i].is_saa)
2174 saa_fpwrite(elf_sects[i].data, ofile);
2175 else
2176 nasm_write(elf_sects[i].data, len, ofile);
2177 fwritezero(align, ofile);
2181 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
2183 saa_wbytes(sect->data, data, len);
2184 sect->len += len;
2187 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
2189 saa_writeaddr(sect->data, data, len);
2190 sect->len += len;
2193 static void elf_sectalign(int32_t seg, unsigned int value)
2195 struct elf_section *s = NULL;
2196 int i;
2198 for (i = 0; i < nsects; i++) {
2199 if (sects[i]->index == seg) {
2200 s = sects[i];
2201 break;
2204 if (!s || !is_power2(value))
2205 return;
2207 if (value > s->align)
2208 s->align = value;
2211 extern macros_t elf_stdmac[];
2213 /* Claim "elf" as a pragma namespace, for the future */
2214 static const struct pragma_facility elf_pragma_list[] =
2216 { "elf", NULL },
2217 { NULL, NULL } /* Implements the canonical output name */
2221 static const struct dfmt elf32_df_dwarf = {
2222 "ELF32 (i386) dwarf debug format for Linux/Unix",
2223 "dwarf",
2224 dwarf_init,
2225 dwarf_linenum,
2226 null_debug_deflabel,
2227 null_debug_directive,
2228 debug_typevalue,
2229 dwarf_output,
2230 dwarf_cleanup,
2231 NULL /* pragma list */
2234 static const struct dfmt elf32_df_stabs = {
2235 "ELF32 (i386) stabs debug format for Linux/Unix",
2236 "stabs",
2237 null_debug_init,
2238 stabs_linenum,
2239 null_debug_deflabel,
2240 null_debug_directive,
2241 debug_typevalue,
2242 stabs_output,
2243 stabs_cleanup,
2244 NULL /* pragma list */
2247 static const struct dfmt * const elf32_debugs_arr[3] =
2248 { &elf32_df_dwarf, &elf32_df_stabs, NULL };
2250 const struct ofmt of_elf32 = {
2251 "ELF32 (i386) object files (e.g. Linux)",
2252 "elf32",
2253 ".o",
2256 elf32_debugs_arr,
2257 &elf32_df_stabs,
2258 elf_stdmac,
2259 elf_init,
2260 null_reset,
2261 nasm_do_legacy_output,
2262 elf32_out,
2263 elf_deflabel,
2264 elf_section_names,
2265 NULL,
2266 elf_sectalign,
2267 null_segbase,
2268 elf_directive,
2269 elf_cleanup,
2270 elf_pragma_list,
2273 static const struct dfmt elf64_df_dwarf = {
2274 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
2275 "dwarf",
2276 dwarf_init,
2277 dwarf_linenum,
2278 null_debug_deflabel,
2279 null_debug_directive,
2280 debug_typevalue,
2281 dwarf_output,
2282 dwarf_cleanup,
2283 NULL /* pragma list */
2286 static const struct dfmt elf64_df_stabs = {
2287 "ELF64 (x86-64) stabs debug format for Linux/Unix",
2288 "stabs",
2289 null_debug_init,
2290 stabs_linenum,
2291 null_debug_deflabel,
2292 null_debug_directive,
2293 debug_typevalue,
2294 stabs_output,
2295 stabs_cleanup,
2296 NULL /* pragma list */
2299 static const struct dfmt * const elf64_debugs_arr[3] =
2300 { &elf64_df_dwarf, &elf64_df_stabs, NULL };
2302 const struct ofmt of_elf64 = {
2303 "ELF64 (x86_64) object files (e.g. Linux)",
2304 "elf64",
2305 ".o",
2308 elf64_debugs_arr,
2309 &elf64_df_stabs,
2310 elf_stdmac,
2311 elf_init,
2312 null_reset,
2313 nasm_do_legacy_output,
2314 elf64_out,
2315 elf_deflabel,
2316 elf_section_names,
2317 NULL,
2318 elf_sectalign,
2319 null_segbase,
2320 elf_directive,
2321 elf_cleanup,
2322 elf_pragma_list,
2325 static const struct dfmt elfx32_df_dwarf = {
2326 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
2327 "dwarf",
2328 dwarf_init,
2329 dwarf_linenum,
2330 null_debug_deflabel,
2331 null_debug_directive,
2332 debug_typevalue,
2333 dwarf_output,
2334 dwarf_cleanup,
2335 NULL /* pragma list */
2338 static const struct dfmt elfx32_df_stabs = {
2339 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
2340 "stabs",
2341 null_debug_init,
2342 stabs_linenum,
2343 null_debug_deflabel,
2344 null_debug_directive,
2345 debug_typevalue,
2346 stabs_output,
2347 stabs_cleanup,
2348 elf_pragma_list,
2351 static const struct dfmt * const elfx32_debugs_arr[3] =
2352 { &elfx32_df_dwarf, &elfx32_df_stabs, NULL };
2354 const struct ofmt of_elfx32 = {
2355 "ELFX32 (x86_64) object files (e.g. Linux)",
2356 "elfx32",
2357 ".o",
2360 elfx32_debugs_arr,
2361 &elfx32_df_stabs,
2362 elf_stdmac,
2363 elf_init,
2364 null_reset,
2365 nasm_do_legacy_output,
2366 elfx32_out,
2367 elf_deflabel,
2368 elf_section_names,
2369 NULL,
2370 elf_sectalign,
2371 null_segbase,
2372 elf_directive,
2373 elf_cleanup,
2374 NULL /* pragma list */
2377 static bool is_elf64(void)
2379 return ofmt == &of_elf64;
2382 static bool is_elf32(void)
2384 return ofmt == &of_elf32;
2387 static bool is_elfx32(void)
2389 return ofmt == &of_elfx32;
2392 static bool dfmt_is_stabs(void)
2394 return dfmt == &elf32_df_stabs ||
2395 dfmt == &elfx32_df_stabs ||
2396 dfmt == &elf64_df_stabs;
2399 static bool dfmt_is_dwarf(void)
2401 return dfmt == &elf32_df_dwarf ||
2402 dfmt == &elfx32_df_dwarf ||
2403 dfmt == &elf64_df_dwarf;
2406 /* common debugging routines */
2407 static void debug_typevalue(int32_t type)
2409 int32_t stype, ssize;
2410 switch (TYM_TYPE(type)) {
2411 case TY_LABEL:
2412 ssize = 0;
2413 stype = STT_NOTYPE;
2414 break;
2415 case TY_BYTE:
2416 ssize = 1;
2417 stype = STT_OBJECT;
2418 break;
2419 case TY_WORD:
2420 ssize = 2;
2421 stype = STT_OBJECT;
2422 break;
2423 case TY_DWORD:
2424 ssize = 4;
2425 stype = STT_OBJECT;
2426 break;
2427 case TY_FLOAT:
2428 ssize = 4;
2429 stype = STT_OBJECT;
2430 break;
2431 case TY_QWORD:
2432 ssize = 8;
2433 stype = STT_OBJECT;
2434 break;
2435 case TY_TBYTE:
2436 ssize = 10;
2437 stype = STT_OBJECT;
2438 break;
2439 case TY_OWORD:
2440 ssize = 16;
2441 stype = STT_OBJECT;
2442 break;
2443 case TY_YWORD:
2444 ssize = 32;
2445 stype = STT_OBJECT;
2446 break;
2447 case TY_ZWORD:
2448 ssize = 64;
2449 stype = STT_OBJECT;
2450 break;
2451 case TY_COMMON:
2452 ssize = 0;
2453 stype = STT_COMMON;
2454 break;
2455 case TY_SEG:
2456 ssize = 0;
2457 stype = STT_SECTION;
2458 break;
2459 case TY_EXTERN:
2460 ssize = 0;
2461 stype = STT_NOTYPE;
2462 break;
2463 case TY_EQU:
2464 ssize = 0;
2465 stype = STT_NOTYPE;
2466 break;
2467 default:
2468 ssize = 0;
2469 stype = STT_NOTYPE;
2470 break;
2472 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
2473 lastsym->size = ssize;
2474 lastsym->type = stype;
2478 /* stabs debugging routines */
2480 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t segto)
2482 (void)segto;
2483 if (!stabs_filename) {
2484 stabs_filename = nasm_malloc(strlen(filename) + 1);
2485 strcpy(stabs_filename, filename);
2486 } else {
2487 if (strcmp(stabs_filename, filename)) {
2488 /* yep, a memory leak...this program is one-shot anyway, so who cares...
2489 in fact, this leak comes in quite handy to maintain a list of files
2490 encountered so far in the symbol lines... */
2492 /* why not nasm_free(stabs_filename); we're done with the old one */
2494 stabs_filename = nasm_malloc(strlen(filename) + 1);
2495 strcpy(stabs_filename, filename);
2498 debug_immcall = 1;
2499 currentline = linenumber;
2502 static void stabs_output(int type, void *param)
2504 struct symlininfo *s;
2505 struct linelist *el;
2506 if (type == TY_DEBUGSYMLIN) {
2507 if (debug_immcall) {
2508 s = (struct symlininfo *)param;
2509 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2510 return; /* line info is only collected for executable sections */
2511 numlinestabs++;
2512 el = nasm_malloc(sizeof(struct linelist));
2513 el->info.offset = s->offset;
2514 el->info.section = s->section;
2515 el->info.name = s->name;
2516 el->line = currentline;
2517 el->filename = stabs_filename;
2518 el->next = 0;
2519 if (stabslines) {
2520 stabslines->last->next = el;
2521 stabslines->last = el;
2522 } else {
2523 stabslines = el;
2524 stabslines->last = el;
2528 debug_immcall = 0;
2531 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
2533 static void stabs_generate(void)
2535 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
2536 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
2537 char **allfiles;
2538 int *fileidx;
2540 struct linelist *ptr;
2542 ptr = stabslines;
2544 allfiles = nasm_zalloc(numlinestabs * sizeof(char *));
2545 numfiles = 0;
2546 while (ptr) {
2547 if (numfiles == 0) {
2548 allfiles[0] = ptr->filename;
2549 numfiles++;
2550 } else {
2551 for (i = 0; i < numfiles; i++) {
2552 if (!strcmp(allfiles[i], ptr->filename))
2553 break;
2555 if (i >= numfiles) {
2556 allfiles[i] = ptr->filename;
2557 numfiles++;
2560 ptr = ptr->next;
2562 strsize = 1;
2563 fileidx = nasm_malloc(numfiles * sizeof(int));
2564 for (i = 0; i < numfiles; i++) {
2565 fileidx[i] = strsize;
2566 strsize += strlen(allfiles[i]) + 1;
2568 currfile = mainfileindex = 0;
2569 for (i = 0; i < numfiles; i++) {
2570 if (!strcmp(allfiles[i], elf_module)) {
2571 currfile = mainfileindex = i;
2572 break;
2577 * worst case size of the stab buffer would be:
2578 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
2579 * plus one "ending" entry
2581 sbuf = nasm_malloc((numlinestabs * 2 + 4) *
2582 sizeof(struct stabentry));
2583 ssbuf = nasm_malloc(strsize);
2584 rbuf = nasm_malloc(numlinestabs * (is_elf64() ? 16 : 8) * (2 + 3));
2585 rptr = rbuf;
2587 for (i = 0; i < numfiles; i++)
2588 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
2589 ssbuf[0] = 0;
2591 stabstrlen = strsize; /* set global variable for length of stab strings */
2593 sptr = sbuf;
2594 ptr = stabslines;
2595 numstabs = 0;
2597 if (ptr) {
2599 * this is the first stab, its strx points to the filename of the
2600 * the source-file, the n_desc field should be set to the number
2601 * of remaining stabs
2603 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
2605 /* this is the stab for the main source file */
2606 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
2608 /* relocation table entry */
2611 * Since the symbol table has two entries before
2612 * the section symbols, the index in the info.section
2613 * member must be adjusted by adding 2
2616 if (is_elf32()) {
2617 WRITELONG(rptr, (sptr - sbuf) - 4);
2618 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2619 } else if (is_elfx32()) {
2620 WRITELONG(rptr, (sptr - sbuf) - 4);
2621 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2622 WRITELONG(rptr, 0);
2623 } else {
2624 nasm_assert(is_elf64());
2625 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2626 WRITELONG(rptr, R_X86_64_32);
2627 WRITELONG(rptr, ptr->info.section + 2);
2628 WRITEDLONG(rptr, 0);
2630 numstabs++;
2633 if (is_elf32()) {
2634 while (ptr) {
2635 if (strcmp(allfiles[currfile], ptr->filename)) {
2636 /* oops file has changed... */
2637 for (i = 0; i < numfiles; i++)
2638 if (!strcmp(allfiles[i], ptr->filename))
2639 break;
2640 currfile = i;
2641 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2642 ptr->info.offset);
2643 numstabs++;
2645 /* relocation table entry */
2646 WRITELONG(rptr, (sptr - sbuf) - 4);
2647 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2650 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2651 numstabs++;
2653 /* relocation table entry */
2654 WRITELONG(rptr, (sptr - sbuf) - 4);
2655 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
2657 ptr = ptr->next;
2659 } else if (is_elfx32()) {
2660 while (ptr) {
2661 if (strcmp(allfiles[currfile], ptr->filename)) {
2662 /* oops file has changed... */
2663 for (i = 0; i < numfiles; i++)
2664 if (!strcmp(allfiles[i], ptr->filename))
2665 break;
2666 currfile = i;
2667 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2668 ptr->info.offset);
2669 numstabs++;
2671 /* relocation table entry */
2672 WRITELONG(rptr, (sptr - sbuf) - 4);
2673 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2674 WRITELONG(rptr, ptr->info.offset);
2677 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2678 numstabs++;
2680 /* relocation table entry */
2681 WRITELONG(rptr, (sptr - sbuf) - 4);
2682 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
2683 WRITELONG(rptr, ptr->info.offset);
2685 ptr = ptr->next;
2687 } else {
2688 nasm_assert(is_elf64());
2689 while (ptr) {
2690 if (strcmp(allfiles[currfile], ptr->filename)) {
2691 /* oops file has changed... */
2692 for (i = 0; i < numfiles; i++)
2693 if (!strcmp(allfiles[i], ptr->filename))
2694 break;
2695 currfile = i;
2696 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
2697 ptr->info.offset);
2698 numstabs++;
2700 /* relocation table entry */
2701 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2702 WRITELONG(rptr, R_X86_64_32);
2703 WRITELONG(rptr, ptr->info.section + 2);
2704 WRITEDLONG(rptr, ptr->info.offset);
2707 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
2708 numstabs++;
2710 /* relocation table entry */
2711 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
2712 WRITELONG(rptr, R_X86_64_32);
2713 WRITELONG(rptr, ptr->info.section + 2);
2714 WRITEDLONG(rptr, ptr->info.offset);
2716 ptr = ptr->next;
2720 /* this is an "ending" token */
2721 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
2722 numstabs++;
2724 ((struct stabentry *)sbuf)->n_desc = numstabs;
2726 nasm_free(allfiles);
2727 nasm_free(fileidx);
2729 stablen = (sptr - sbuf);
2730 stabrellen = (rptr - rbuf);
2731 stabrelbuf = rbuf;
2732 stabbuf = sbuf;
2733 stabstrbuf = ssbuf;
2736 static void stabs_cleanup(void)
2738 struct linelist *ptr, *del;
2739 if (!stabslines)
2740 return;
2742 ptr = stabslines;
2743 while (ptr) {
2744 del = ptr;
2745 ptr = ptr->next;
2746 nasm_free(del);
2749 nasm_free(stabbuf);
2750 nasm_free(stabrelbuf);
2751 nasm_free(stabstrbuf);
2754 /* dwarf routines */
2756 static void dwarf_init(void)
2758 ndebugs = 3; /* 3 debug symbols */
2761 static void dwarf_linenum(const char *filename, int32_t linenumber,
2762 int32_t segto)
2764 (void)segto;
2765 dwarf_findfile(filename);
2766 debug_immcall = 1;
2767 currentline = linenumber;
2770 /* called from elf_out with type == TY_DEBUGSYMLIN */
2771 static void dwarf_output(int type, void *param)
2773 int ln, aa, inx, maxln, soc;
2774 struct symlininfo *s;
2775 struct SAA *plinep;
2777 (void)type;
2779 s = (struct symlininfo *)param;
2781 /* line number info is only gathered for executable sections */
2782 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2783 return;
2785 /* Check if section index has changed */
2786 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
2787 dwarf_findsect(s->section);
2789 /* do nothing unless line or file has changed */
2790 if (!debug_immcall)
2791 return;
2793 ln = currentline - dwarf_csect->line;
2794 aa = s->offset - dwarf_csect->offset;
2795 inx = dwarf_clist->line;
2796 plinep = dwarf_csect->psaa;
2797 /* check for file change */
2798 if (!(inx == dwarf_csect->file)) {
2799 saa_write8(plinep,DW_LNS_set_file);
2800 saa_write8(plinep,inx);
2801 dwarf_csect->file = inx;
2803 /* check for line change */
2804 if (ln) {
2805 /* test if in range of special op code */
2806 maxln = line_base + line_range;
2807 soc = (ln - line_base) + (line_range * aa) + opcode_base;
2808 if (ln >= line_base && ln < maxln && soc < 256) {
2809 saa_write8(plinep,soc);
2810 } else {
2811 saa_write8(plinep,DW_LNS_advance_line);
2812 saa_wleb128s(plinep,ln);
2813 if (aa) {
2814 saa_write8(plinep,DW_LNS_advance_pc);
2815 saa_wleb128u(plinep,aa);
2817 saa_write8(plinep,DW_LNS_copy);
2819 dwarf_csect->line = currentline;
2820 dwarf_csect->offset = s->offset;
2823 /* show change handled */
2824 debug_immcall = 0;
2828 static void dwarf_generate(void)
2830 uint8_t *pbuf;
2831 int indx;
2832 struct linelist *ftentry;
2833 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
2834 struct SAA *parangesrel, *plinesrel, *pinforel;
2835 struct sectlist *psect;
2836 size_t saalen, linepoff, totlen, highaddr;
2838 if (is_elf32()) {
2839 /* write epilogues for each line program range */
2840 /* and build aranges section */
2841 paranges = saa_init(1L);
2842 parangesrel = saa_init(1L);
2843 saa_write16(paranges,2); /* dwarf version */
2844 saa_write32(parangesrel, paranges->datalen+4);
2845 saa_write32(parangesrel, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
2846 saa_write32(parangesrel, 0);
2847 saa_write32(paranges,0); /* offset into info */
2848 saa_write8(paranges,4); /* pointer size */
2849 saa_write8(paranges,0); /* not segmented */
2850 saa_write32(paranges,0); /* padding */
2851 /* iterate though sectlist entries */
2852 psect = dwarf_fsect;
2853 totlen = 0;
2854 highaddr = 0;
2855 for (indx = 0; indx < dwarf_nsections; indx++) {
2856 plinep = psect->psaa;
2857 /* Line Number Program Epilogue */
2858 saa_write8(plinep,2); /* std op 2 */
2859 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2860 saa_write8(plinep,DW_LNS_extended_op);
2861 saa_write8(plinep,1); /* operand length */
2862 saa_write8(plinep,DW_LNE_end_sequence);
2863 totlen += plinep->datalen;
2864 /* range table relocation entry */
2865 saa_write32(parangesrel, paranges->datalen + 4);
2866 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2867 saa_write32(parangesrel, (uint32_t) 0);
2868 /* range table entry */
2869 saa_write32(paranges,0x0000); /* range start */
2870 saa_write32(paranges,sects[psect->section]->len); /* range length */
2871 highaddr += sects[psect->section]->len;
2872 /* done with this entry */
2873 psect = psect->next;
2875 saa_write32(paranges,0); /* null address */
2876 saa_write32(paranges,0); /* null length */
2877 saalen = paranges->datalen;
2878 arangeslen = saalen + 4;
2879 arangesbuf = pbuf = nasm_malloc(arangeslen);
2880 WRITELONG(pbuf,saalen); /* initial length */
2881 saa_rnbytes(paranges, pbuf, saalen);
2882 saa_free(paranges);
2883 } else if (is_elfx32()) {
2884 /* write epilogues for each line program range */
2885 /* and build aranges section */
2886 paranges = saa_init(1L);
2887 parangesrel = saa_init(1L);
2888 saa_write16(paranges,3); /* dwarf version */
2889 saa_write32(parangesrel, paranges->datalen+4);
2890 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
2891 saa_write32(parangesrel, 0);
2892 saa_write32(paranges,0); /* offset into info */
2893 saa_write8(paranges,4); /* pointer size */
2894 saa_write8(paranges,0); /* not segmented */
2895 saa_write32(paranges,0); /* padding */
2896 /* iterate though sectlist entries */
2897 psect = dwarf_fsect;
2898 totlen = 0;
2899 highaddr = 0;
2900 for (indx = 0; indx < dwarf_nsections; indx++) {
2901 plinep = psect->psaa;
2902 /* Line Number Program Epilogue */
2903 saa_write8(plinep,2); /* std op 2 */
2904 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2905 saa_write8(plinep,DW_LNS_extended_op);
2906 saa_write8(plinep,1); /* operand length */
2907 saa_write8(plinep,DW_LNE_end_sequence);
2908 totlen += plinep->datalen;
2909 /* range table relocation entry */
2910 saa_write32(parangesrel, paranges->datalen + 4);
2911 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
2912 saa_write32(parangesrel, (uint32_t) 0);
2913 /* range table entry */
2914 saa_write32(paranges,0x0000); /* range start */
2915 saa_write32(paranges,sects[psect->section]->len); /* range length */
2916 highaddr += sects[psect->section]->len;
2917 /* done with this entry */
2918 psect = psect->next;
2920 saa_write32(paranges,0); /* null address */
2921 saa_write32(paranges,0); /* null length */
2922 saalen = paranges->datalen;
2923 arangeslen = saalen + 4;
2924 arangesbuf = pbuf = nasm_malloc(arangeslen);
2925 WRITELONG(pbuf,saalen); /* initial length */
2926 saa_rnbytes(paranges, pbuf, saalen);
2927 saa_free(paranges);
2928 } else {
2929 nasm_assert(is_elf64());
2930 /* write epilogues for each line program range */
2931 /* and build aranges section */
2932 paranges = saa_init(1L);
2933 parangesrel = saa_init(1L);
2934 saa_write16(paranges,3); /* dwarf version */
2935 saa_write64(parangesrel, paranges->datalen+4);
2936 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
2937 saa_write64(parangesrel, 0);
2938 saa_write32(paranges,0); /* offset into info */
2939 saa_write8(paranges,8); /* pointer size */
2940 saa_write8(paranges,0); /* not segmented */
2941 saa_write32(paranges,0); /* padding */
2942 /* iterate though sectlist entries */
2943 psect = dwarf_fsect;
2944 totlen = 0;
2945 highaddr = 0;
2946 for (indx = 0; indx < dwarf_nsections; indx++) {
2947 plinep = psect->psaa;
2948 /* Line Number Program Epilogue */
2949 saa_write8(plinep,2); /* std op 2 */
2950 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
2951 saa_write8(plinep,DW_LNS_extended_op);
2952 saa_write8(plinep,1); /* operand length */
2953 saa_write8(plinep,DW_LNE_end_sequence);
2954 totlen += plinep->datalen;
2955 /* range table relocation entry */
2956 saa_write64(parangesrel, paranges->datalen + 4);
2957 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2958 saa_write64(parangesrel, (uint64_t) 0);
2959 /* range table entry */
2960 saa_write64(paranges,0x0000); /* range start */
2961 saa_write64(paranges,sects[psect->section]->len); /* range length */
2962 highaddr += sects[psect->section]->len;
2963 /* done with this entry */
2964 psect = psect->next;
2966 saa_write64(paranges,0); /* null address */
2967 saa_write64(paranges,0); /* null length */
2968 saalen = paranges->datalen;
2969 arangeslen = saalen + 4;
2970 arangesbuf = pbuf = nasm_malloc(arangeslen);
2971 WRITELONG(pbuf,saalen); /* initial length */
2972 saa_rnbytes(paranges, pbuf, saalen);
2973 saa_free(paranges);
2976 /* build rela.aranges section */
2977 arangesrellen = saalen = parangesrel->datalen;
2978 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
2979 saa_rnbytes(parangesrel, pbuf, saalen);
2980 saa_free(parangesrel);
2982 /* build pubnames section */
2983 ppubnames = saa_init(1L);
2984 saa_write16(ppubnames,3); /* dwarf version */
2985 saa_write32(ppubnames,0); /* offset into info */
2986 saa_write32(ppubnames,0); /* space used in info */
2987 saa_write32(ppubnames,0); /* end of list */
2988 saalen = ppubnames->datalen;
2989 pubnameslen = saalen + 4;
2990 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2991 WRITELONG(pbuf,saalen); /* initial length */
2992 saa_rnbytes(ppubnames, pbuf, saalen);
2993 saa_free(ppubnames);
2995 if (is_elf32()) {
2996 /* build info section */
2997 pinfo = saa_init(1L);
2998 pinforel = saa_init(1L);
2999 saa_write16(pinfo,2); /* dwarf version */
3000 saa_write32(pinforel, pinfo->datalen + 4);
3001 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
3002 saa_write32(pinforel, 0);
3003 saa_write32(pinfo,0); /* offset into abbrev */
3004 saa_write8(pinfo,4); /* pointer size */
3005 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3006 saa_write32(pinforel, pinfo->datalen + 4);
3007 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3008 saa_write32(pinforel, 0);
3009 saa_write32(pinfo,0); /* DW_AT_low_pc */
3010 saa_write32(pinforel, pinfo->datalen + 4);
3011 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3012 saa_write32(pinforel, 0);
3013 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
3014 saa_write32(pinforel, pinfo->datalen + 4);
3015 saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
3016 saa_write32(pinforel, 0);
3017 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3018 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3019 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3020 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3021 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3022 saa_write32(pinforel, pinfo->datalen + 4);
3023 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_386_32);
3024 saa_write32(pinforel, 0);
3025 saa_write32(pinfo,0); /* DW_AT_low_pc */
3026 saa_write32(pinfo,0); /* DW_AT_frame_base */
3027 saa_write8(pinfo,0); /* end of entries */
3028 saalen = pinfo->datalen;
3029 infolen = saalen + 4;
3030 infobuf = pbuf = nasm_malloc(infolen);
3031 WRITELONG(pbuf,saalen); /* initial length */
3032 saa_rnbytes(pinfo, pbuf, saalen);
3033 saa_free(pinfo);
3034 } else if (is_elfx32()) {
3035 /* build info section */
3036 pinfo = saa_init(1L);
3037 pinforel = saa_init(1L);
3038 saa_write16(pinfo,3); /* dwarf version */
3039 saa_write32(pinforel, pinfo->datalen + 4);
3040 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
3041 saa_write32(pinforel, 0);
3042 saa_write32(pinfo,0); /* offset into abbrev */
3043 saa_write8(pinfo,4); /* pointer size */
3044 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3045 saa_write32(pinforel, pinfo->datalen + 4);
3046 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3047 saa_write32(pinforel, 0);
3048 saa_write32(pinfo,0); /* DW_AT_low_pc */
3049 saa_write32(pinforel, pinfo->datalen + 4);
3050 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3051 saa_write32(pinforel, 0);
3052 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
3053 saa_write32(pinforel, pinfo->datalen + 4);
3054 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
3055 saa_write32(pinforel, 0);
3056 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3057 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3058 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3059 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3060 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3061 saa_write32(pinforel, pinfo->datalen + 4);
3062 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
3063 saa_write32(pinforel, 0);
3064 saa_write32(pinfo,0); /* DW_AT_low_pc */
3065 saa_write32(pinfo,0); /* DW_AT_frame_base */
3066 saa_write8(pinfo,0); /* end of entries */
3067 saalen = pinfo->datalen;
3068 infolen = saalen + 4;
3069 infobuf = pbuf = nasm_malloc(infolen);
3070 WRITELONG(pbuf,saalen); /* initial length */
3071 saa_rnbytes(pinfo, pbuf, saalen);
3072 saa_free(pinfo);
3073 } else {
3074 nasm_assert(is_elf64());
3075 /* build info section */
3076 pinfo = saa_init(1L);
3077 pinforel = saa_init(1L);
3078 saa_write16(pinfo,3); /* dwarf version */
3079 saa_write64(pinforel, pinfo->datalen + 4);
3080 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
3081 saa_write64(pinforel, 0);
3082 saa_write32(pinfo,0); /* offset into abbrev */
3083 saa_write8(pinfo,8); /* pointer size */
3084 saa_write8(pinfo,1); /* abbrviation number LEB128u */
3085 saa_write64(pinforel, pinfo->datalen + 4);
3086 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3087 saa_write64(pinforel, 0);
3088 saa_write64(pinfo,0); /* DW_AT_low_pc */
3089 saa_write64(pinforel, pinfo->datalen + 4);
3090 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3091 saa_write64(pinforel, 0);
3092 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
3093 saa_write64(pinforel, pinfo->datalen + 4);
3094 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
3095 saa_write64(pinforel, 0);
3096 saa_write32(pinfo,0); /* DW_AT_stmt_list */
3097 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3098 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
3099 saa_write16(pinfo,DW_LANG_Mips_Assembler);
3100 saa_write8(pinfo,2); /* abbrviation number LEB128u */
3101 saa_write64(pinforel, pinfo->datalen + 4);
3102 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
3103 saa_write64(pinforel, 0);
3104 saa_write64(pinfo,0); /* DW_AT_low_pc */
3105 saa_write64(pinfo,0); /* DW_AT_frame_base */
3106 saa_write8(pinfo,0); /* end of entries */
3107 saalen = pinfo->datalen;
3108 infolen = saalen + 4;
3109 infobuf = pbuf = nasm_malloc(infolen);
3110 WRITELONG(pbuf,saalen); /* initial length */
3111 saa_rnbytes(pinfo, pbuf, saalen);
3112 saa_free(pinfo);
3115 /* build rela.info section */
3116 inforellen = saalen = pinforel->datalen;
3117 inforelbuf = pbuf = nasm_malloc(inforellen);
3118 saa_rnbytes(pinforel, pbuf, saalen);
3119 saa_free(pinforel);
3121 /* build abbrev section */
3122 pabbrev = saa_init(1L);
3123 saa_write8(pabbrev,1); /* entry number LEB128u */
3124 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
3125 saa_write8(pabbrev,1); /* has children */
3126 /* the following attributes and forms are all LEB128u values */
3127 saa_write8(pabbrev,DW_AT_low_pc);
3128 saa_write8(pabbrev,DW_FORM_addr);
3129 saa_write8(pabbrev,DW_AT_high_pc);
3130 saa_write8(pabbrev,DW_FORM_addr);
3131 saa_write8(pabbrev,DW_AT_stmt_list);
3132 saa_write8(pabbrev,DW_FORM_data4);
3133 saa_write8(pabbrev,DW_AT_name);
3134 saa_write8(pabbrev,DW_FORM_string);
3135 saa_write8(pabbrev,DW_AT_producer);
3136 saa_write8(pabbrev,DW_FORM_string);
3137 saa_write8(pabbrev,DW_AT_language);
3138 saa_write8(pabbrev,DW_FORM_data2);
3139 saa_write16(pabbrev,0); /* end of entry */
3140 /* LEB128u usage same as above */
3141 saa_write8(pabbrev,2); /* entry number */
3142 saa_write8(pabbrev,DW_TAG_subprogram);
3143 saa_write8(pabbrev,0); /* no children */
3144 saa_write8(pabbrev,DW_AT_low_pc);
3145 saa_write8(pabbrev,DW_FORM_addr);
3146 saa_write8(pabbrev,DW_AT_frame_base);
3147 saa_write8(pabbrev,DW_FORM_data4);
3148 saa_write16(pabbrev,0); /* end of entry */
3149 /* Terminal zero entry */
3150 saa_write8(pabbrev,0);
3151 abbrevlen = saalen = pabbrev->datalen;
3152 abbrevbuf = pbuf = nasm_malloc(saalen);
3153 saa_rnbytes(pabbrev, pbuf, saalen);
3154 saa_free(pabbrev);
3156 /* build line section */
3157 /* prolog */
3158 plines = saa_init(1L);
3159 saa_write8(plines,1); /* Minimum Instruction Length */
3160 saa_write8(plines,1); /* Initial value of 'is_stmt' */
3161 saa_write8(plines,line_base); /* Line Base */
3162 saa_write8(plines,line_range); /* Line Range */
3163 saa_write8(plines,opcode_base); /* Opcode Base */
3164 /* standard opcode lengths (# of LEB128u operands) */
3165 saa_write8(plines,0); /* Std opcode 1 length */
3166 saa_write8(plines,1); /* Std opcode 2 length */
3167 saa_write8(plines,1); /* Std opcode 3 length */
3168 saa_write8(plines,1); /* Std opcode 4 length */
3169 saa_write8(plines,1); /* Std opcode 5 length */
3170 saa_write8(plines,0); /* Std opcode 6 length */
3171 saa_write8(plines,0); /* Std opcode 7 length */
3172 saa_write8(plines,0); /* Std opcode 8 length */
3173 saa_write8(plines,1); /* Std opcode 9 length */
3174 saa_write8(plines,0); /* Std opcode 10 length */
3175 saa_write8(plines,0); /* Std opcode 11 length */
3176 saa_write8(plines,1); /* Std opcode 12 length */
3177 /* Directory Table */
3178 saa_write8(plines,0); /* End of table */
3179 /* File Name Table */
3180 ftentry = dwarf_flist;
3181 for (indx = 0; indx < dwarf_numfiles; indx++) {
3182 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
3183 saa_write8(plines,0); /* directory LEB128u */
3184 saa_write8(plines,0); /* time LEB128u */
3185 saa_write8(plines,0); /* size LEB128u */
3186 ftentry = ftentry->next;
3188 saa_write8(plines,0); /* End of table */
3189 linepoff = plines->datalen;
3190 linelen = linepoff + totlen + 10;
3191 linebuf = pbuf = nasm_malloc(linelen);
3192 WRITELONG(pbuf,linelen-4); /* initial length */
3193 WRITESHORT(pbuf,3); /* dwarf version */
3194 WRITELONG(pbuf,linepoff); /* offset to line number program */
3195 /* write line header */
3196 saalen = linepoff;
3197 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
3198 pbuf += linepoff;
3199 saa_free(plines);
3200 /* concatonate line program ranges */
3201 linepoff += 13;
3202 plinesrel = saa_init(1L);
3203 psect = dwarf_fsect;
3204 if (is_elf32()) {
3205 for (indx = 0; indx < dwarf_nsections; indx++) {
3206 saa_write32(plinesrel, linepoff);
3207 saa_write32(plinesrel, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
3208 saa_write32(plinesrel, (uint32_t) 0);
3209 plinep = psect->psaa;
3210 saalen = plinep->datalen;
3211 saa_rnbytes(plinep, pbuf, saalen);
3212 pbuf += saalen;
3213 linepoff += saalen;
3214 saa_free(plinep);
3215 /* done with this entry */
3216 psect = psect->next;
3218 } else if (is_elfx32()) {
3219 for (indx = 0; indx < dwarf_nsections; indx++) {
3220 saa_write32(plinesrel, linepoff);
3221 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
3222 saa_write32(plinesrel, 0);
3223 plinep = psect->psaa;
3224 saalen = plinep->datalen;
3225 saa_rnbytes(plinep, pbuf, saalen);
3226 pbuf += saalen;
3227 linepoff += saalen;
3228 saa_free(plinep);
3229 /* done with this entry */
3230 psect = psect->next;
3232 } else {
3233 nasm_assert(is_elf64());
3234 for (indx = 0; indx < dwarf_nsections; indx++) {
3235 saa_write64(plinesrel, linepoff);
3236 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
3237 saa_write64(plinesrel, (uint64_t) 0);
3238 plinep = psect->psaa;
3239 saalen = plinep->datalen;
3240 saa_rnbytes(plinep, pbuf, saalen);
3241 pbuf += saalen;
3242 linepoff += saalen;
3243 saa_free(plinep);
3244 /* done with this entry */
3245 psect = psect->next;
3249 /* build rela.lines section */
3250 linerellen =saalen = plinesrel->datalen;
3251 linerelbuf = pbuf = nasm_malloc(linerellen);
3252 saa_rnbytes(plinesrel, pbuf, saalen);
3253 saa_free(plinesrel);
3255 /* build frame section */
3256 framelen = 4;
3257 framebuf = pbuf = nasm_malloc(framelen);
3258 WRITELONG(pbuf,framelen-4); /* initial length */
3260 /* build loc section */
3261 loclen = 16;
3262 locbuf = pbuf = nasm_malloc(loclen);
3263 if (is_elf32()) {
3264 WRITELONG(pbuf,0); /* null beginning offset */
3265 WRITELONG(pbuf,0); /* null ending offset */
3266 } else if (is_elfx32()) {
3267 WRITELONG(pbuf,0); /* null beginning offset */
3268 WRITELONG(pbuf,0); /* null ending offset */
3269 } else {
3270 nasm_assert(is_elf64());
3271 WRITEDLONG(pbuf,0); /* null beginning offset */
3272 WRITEDLONG(pbuf,0); /* null ending offset */
3276 static void dwarf_cleanup(void)
3278 nasm_free(arangesbuf);
3279 nasm_free(arangesrelbuf);
3280 nasm_free(pubnamesbuf);
3281 nasm_free(infobuf);
3282 nasm_free(inforelbuf);
3283 nasm_free(abbrevbuf);
3284 nasm_free(linebuf);
3285 nasm_free(linerelbuf);
3286 nasm_free(framebuf);
3287 nasm_free(locbuf);
3290 static void dwarf_findfile(const char * fname)
3292 int finx;
3293 struct linelist *match;
3295 /* return if fname is current file name */
3296 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
3297 return;
3299 /* search for match */
3300 match = 0;
3301 if (dwarf_flist) {
3302 match = dwarf_flist;
3303 for (finx = 0; finx < dwarf_numfiles; finx++) {
3304 if (!(strcmp(fname, match->filename))) {
3305 dwarf_clist = match;
3306 return;
3308 match = match->next;
3312 /* add file name to end of list */
3313 dwarf_clist = nasm_malloc(sizeof(struct linelist));
3314 dwarf_numfiles++;
3315 dwarf_clist->line = dwarf_numfiles;
3316 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
3317 strcpy(dwarf_clist->filename,fname);
3318 dwarf_clist->next = 0;
3319 if (!dwarf_flist) { /* if first entry */
3320 dwarf_flist = dwarf_elist = dwarf_clist;
3321 dwarf_clist->last = 0;
3322 } else { /* chain to previous entry */
3323 dwarf_elist->next = dwarf_clist;
3324 dwarf_elist = dwarf_clist;
3328 static void dwarf_findsect(const int index)
3330 int sinx;
3331 struct sectlist *match;
3332 struct SAA *plinep;
3334 /* return if index is current section index */
3335 if (dwarf_csect && (dwarf_csect->section == index))
3336 return;
3338 /* search for match */
3339 match = 0;
3340 if (dwarf_fsect) {
3341 match = dwarf_fsect;
3342 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
3343 if (match->section == index) {
3344 dwarf_csect = match;
3345 return;
3347 match = match->next;
3351 /* add entry to end of list */
3352 dwarf_csect = nasm_malloc(sizeof(struct sectlist));
3353 dwarf_nsections++;
3354 dwarf_csect->psaa = plinep = saa_init(1L);
3355 dwarf_csect->line = 1;
3356 dwarf_csect->offset = 0;
3357 dwarf_csect->file = 1;
3358 dwarf_csect->section = index;
3359 dwarf_csect->next = 0;
3360 /* set relocatable address at start of line program */
3361 saa_write8(plinep,DW_LNS_extended_op);
3362 saa_write8(plinep,is_elf64() ? 9 : 5); /* operand length */
3363 saa_write8(plinep,DW_LNE_set_address);
3364 if (is_elf64())
3365 saa_write64(plinep,0); /* Start Address */
3366 else
3367 saa_write32(plinep,0); /* Start Address */
3369 if (!dwarf_fsect) { /* if first entry */
3370 dwarf_fsect = dwarf_esect = dwarf_csect;
3371 dwarf_csect->last = 0;
3372 } else { /* chain to previous entry */
3373 dwarf_esect->next = dwarf_csect;
3374 dwarf_esect = dwarf_csect;
3378 #endif /* defined(OF_ELF32) || defined(OF_ELF64) || defined(OF_ELFX32) */