Makefiles: run "make alldeps"
[nasm.git] / output / outelf64.c
blob878c8e49b03f0f63749bea0776678a61f73b0e38
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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 * outelf64.c output routines for the Netwide Assembler to produce
36 * ELF64 (x86_64 of course) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
45 #include <inttypes.h>
47 #include "nasm.h"
48 #include "nasmlib.h"
49 #include "saa.h"
50 #include "raa.h"
51 #include "stdscan.h"
52 #include "eval.h"
53 #include "output/outform.h"
54 #include "output/outlib.h"
55 #include "rbtree.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELF64
63 #define SECT_DELTA 32
64 static struct elf_section **sects;
65 static int nsects, sectlen;
67 #define SHSTR_DELTA 256
68 static char *shstrtab;
69 static int shstrtablen, shstrtabsize;
71 static struct SAA *syms;
72 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
74 static int32_t def_seg;
76 static struct RAA *bsym;
78 static struct SAA *strs;
79 static uint32_t strslen;
81 static struct elf_symbol *fwds;
83 static char elf_module[FILENAME_MAX];
85 extern struct ofmt of_elf64;
87 static struct ELF_SECTDATA {
88 void *data;
89 int64_t len;
90 bool is_saa;
91 } *elf_sects;
92 static int elf_nsect, nsections;
93 static int64_t elf_foffs;
95 static void elf_write(void);
96 static void elf_sect_write(struct elf_section *, const void *, size_t);
97 static void elf_sect_writeaddr(struct elf_section *, int64_t, size_t);
98 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
99 int, int);
100 static void elf_write_sections(void);
101 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
102 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
103 static void add_sectname(char *, char *);
105 struct erel {
106 int offset, info;
109 struct symlininfo {
110 int offset;
111 int section; /* index into sects[] */
112 int segto; /* internal section number */
113 char *name; /* shallow-copied pointer of section name */
116 struct linelist {
117 struct linelist *next;
118 struct linelist *last;
119 struct symlininfo info;
120 char *filename;
121 int line;
124 struct sectlist {
125 struct SAA *psaa;
126 int section;
127 int line;
128 int offset;
129 int file;
130 struct sectlist *next;
131 struct sectlist *last;
134 /* common debug variables */
135 static int currentline = 1;
136 static int debug_immcall = 0;
138 /* stabs debug variables */
139 static struct linelist *stabslines = 0;
140 static int numlinestabs = 0;
141 static char *stabs_filename = 0;
142 static int symtabsection;
143 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
144 static int stablen, stabstrlen, stabrellen;
146 /* dwarf debug variables */
147 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
148 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
149 static int dwarf_numfiles = 0, dwarf_nsections;
150 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
151 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
152 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
153 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
154 abbrevlen, linelen, linerellen, framelen, loclen;
155 static int64_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
158 static struct dfmt df_dwarf;
159 static struct dfmt df_stabs;
160 static struct elf_symbol *lastsym;
162 /* common debugging routines */
163 static void debug64_typevalue(int32_t);
165 /* stabs debugging routines */
166 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
167 static void stabs64_output(int, void *);
168 static void stabs64_generate(void);
169 static void stabs64_cleanup(void);
171 /* dwarf debugging routines */
172 static void dwarf64_init(void);
173 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
174 static void dwarf64_output(int, void *);
175 static void dwarf64_generate(void);
176 static void dwarf64_cleanup(void);
177 static void dwarf64_findfile(const char *);
178 static void dwarf64_findsect(const int);
181 * Special section numbers which are used to define ELF special
182 * symbols, which can be used with WRT to provide PIC relocation
183 * types.
185 static int32_t elf_gotpc_sect, elf_gotoff_sect;
186 static int32_t elf_got_sect, elf_plt_sect;
187 static int32_t elf_sym_sect;
188 static int32_t elf_gottpoff_sect;
190 static void elf_init(void)
192 sects = NULL;
193 nsects = sectlen = 0;
194 syms = saa_init((int32_t)sizeof(struct elf_symbol));
195 nlocals = nglobs = ndebugs = 0;
196 bsym = raa_init();
197 strs = saa_init(1L);
198 saa_wbytes(strs, "\0", 1L);
199 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
200 strslen = 2 + strlen(elf_module);
201 shstrtab = NULL;
202 shstrtablen = shstrtabsize = 0;;
203 add_sectname("", "");
205 fwds = NULL;
207 elf_gotpc_sect = seg_alloc();
208 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
209 elf_gotoff_sect = seg_alloc();
210 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
211 elf_got_sect = seg_alloc();
212 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
213 elf_plt_sect = seg_alloc();
214 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
215 elf_sym_sect = seg_alloc();
216 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
217 elf_gottpoff_sect = seg_alloc();
218 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
220 def_seg = seg_alloc();
224 static void elf_cleanup(void)
226 struct elf_reloc *r;
227 int i;
229 elf_write();
230 for (i = 0; i < nsects; i++) {
231 if (sects[i]->type != SHT_NOBITS)
232 saa_free(sects[i]->data);
233 if (sects[i]->head)
234 saa_free(sects[i]->rel);
235 while (sects[i]->head) {
236 r = sects[i]->head;
237 sects[i]->head = sects[i]->head->next;
238 nasm_free(r);
241 nasm_free(sects);
242 saa_free(syms);
243 raa_free(bsym);
244 saa_free(strs);
245 dfmt->cleanup();
248 /* add entry to the elf .shstrtab section */
249 static void add_sectname(char *firsthalf, char *secondhalf)
251 int len = strlen(firsthalf) + strlen(secondhalf);
252 while (shstrtablen + len + 1 > shstrtabsize)
253 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
254 strcpy(shstrtab + shstrtablen, firsthalf);
255 strcat(shstrtab + shstrtablen, secondhalf);
256 shstrtablen += len + 1;
259 static int elf_make_section(char *name, int type, int flags, int align)
261 struct elf_section *s;
263 s = nasm_zalloc(sizeof(*s));
265 if (type != SHT_NOBITS)
266 s->data = saa_init(1L);
267 s->tail = &s->head;
268 if (!strcmp(name, ".text"))
269 s->index = def_seg;
270 else
271 s->index = seg_alloc();
272 add_sectname("", name);
274 s->name = nasm_strdup(name);
275 s->type = type;
276 s->flags = flags;
277 s->align = align;
279 if (nsects >= sectlen)
280 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
281 sects[nsects++] = s;
283 return nsects - 1;
286 static int32_t elf_section_names(char *name, int pass, int *bits)
288 char *p;
289 uint32_t flags, flags_and, flags_or;
290 uint64_t align;
291 int type, i;
294 * Default is 64 bits.
296 if (!name) {
297 *bits = 64;
298 return def_seg;
301 p = nasm_skip_word(name);
302 if (*p)
303 *p++ = '\0';
304 flags_and = flags_or = type = align = 0;
306 elf_section_attrib(name, p, pass, &flags_and,
307 &flags_or, &align, &type);
309 if (!strcmp(name, ".shstrtab") ||
310 !strcmp(name, ".symtab") ||
311 !strcmp(name, ".strtab")) {
312 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
313 "name `%s'", name);
314 return NO_SEG;
317 for (i = 0; i < nsects; i++)
318 if (!strcmp(name, sects[i]->name))
319 break;
320 if (i == nsects) {
321 const struct elf_known_section *ks = elf_known_sections;
323 while (ks->name) {
324 if (!strcmp(name, ks->name))
325 break;
326 ks++;
329 type = type ? type : ks->type;
330 align = align ? align : ks->align;
331 flags = (ks->flags & ~flags_and) | flags_or;
333 i = elf_make_section(name, type, flags, align);
334 } else if (pass == 1) {
335 if ((type && sects[i]->type != type)
336 || (align && sects[i]->align != align)
337 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
338 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
339 " redeclaration of section `%s'", name);
342 return sects[i]->index;
345 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
346 int is_global, char *special)
348 int pos = strslen;
349 struct elf_symbol *sym;
350 bool special_used = false;
352 #if defined(DEBUG) && DEBUG>2
353 nasm_error(ERR_DEBUG,
354 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
355 name, segment, offset, is_global, special);
356 #endif
357 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
359 * This is a NASM special symbol. We never allow it into
360 * the ELF symbol table, even if it's a valid one. If it
361 * _isn't_ a valid one, we should barf immediately.
363 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
364 strcmp(name, "..got") && strcmp(name, "..plt") &&
365 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
366 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
367 return;
370 if (is_global == 3) {
371 struct elf_symbol **s;
373 * Fix up a forward-reference symbol size from the first
374 * pass.
376 for (s = &fwds; *s; s = &(*s)->nextfwd)
377 if (!strcmp((*s)->name, name)) {
378 struct tokenval tokval;
379 expr *e;
380 char *p = nasm_skip_spaces(nasm_skip_word(special));
382 stdscan_reset();
383 stdscan_set(p);
384 tokval.t_type = TOKEN_INVALID;
385 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
386 if (e) {
387 if (!is_simple(e))
388 nasm_error(ERR_NONFATAL, "cannot use relocatable"
389 " expression as symbol size");
390 else
391 (*s)->size = reloc_value(e);
395 * Remove it from the list of unresolved sizes.
397 nasm_free((*s)->name);
398 *s = (*s)->nextfwd;
399 return;
401 return; /* it wasn't an important one */
404 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
405 strslen += 1 + strlen(name);
407 lastsym = sym = saa_wstruct(syms);
409 memset(&sym->symv, 0, sizeof(struct rbtree));
411 sym->strpos = pos;
412 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
413 sym->other = STV_DEFAULT;
414 sym->size = 0;
415 if (segment == NO_SEG)
416 sym->section = SHN_ABS;
417 else {
418 int i;
419 sym->section = SHN_UNDEF;
420 if (segment == def_seg) {
421 /* we have to be sure at least text section is there */
422 int tempint;
423 if (segment != elf_section_names(".text", 2, &tempint))
424 nasm_panic(0, "strange segment conditions in ELF driver");
426 for (i = 0; i < nsects; i++) {
427 if (segment == sects[i]->index) {
428 sym->section = i + 1;
429 break;
434 if (is_global == 2) {
435 sym->size = offset;
436 sym->symv.key = 0;
437 sym->section = SHN_COMMON;
439 * We have a common variable. Check the special text to see
440 * if it's a valid number and power of two; if so, store it
441 * as the alignment for the common variable.
443 if (special) {
444 bool err;
445 sym->symv.key = readnum(special, &err);
446 if (err)
447 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
448 " valid number", special);
449 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
450 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
451 " power of two", special);
453 special_used = true;
454 } else
455 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
457 if (sym->type == SYM_GLOBAL) {
459 * If sym->section == SHN_ABS, then the first line of the
460 * else section would cause a core dump, because its a reference
461 * beyond the end of the section array.
462 * This behaviour is exhibited by this code:
463 * GLOBAL crash_nasm
464 * crash_nasm equ 0
465 * To avoid such a crash, such requests are silently discarded.
466 * This may not be the best solution.
468 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
469 bsym = raa_write(bsym, segment, nglobs);
470 } else if (sym->section != SHN_ABS) {
472 * This is a global symbol; so we must add it to the rbtree
473 * of global symbols in its section.
475 * In addition, we check the special text for symbol
476 * type and size information.
478 sects[sym->section-1]->gsyms =
479 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
481 if (special) {
482 int n = strcspn(special, " \t");
484 if (!nasm_strnicmp(special, "function", n))
485 sym->type |= STT_FUNC;
486 else if (!nasm_strnicmp(special, "data", n) ||
487 !nasm_strnicmp(special, "object", n))
488 sym->type |= STT_OBJECT;
489 else if (!nasm_strnicmp(special, "notype", n))
490 sym->type |= STT_NOTYPE;
491 else
492 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
493 n, special);
494 special += n;
496 special = nasm_skip_spaces(special);
497 if (*special) {
498 n = strcspn(special, " \t");
499 if (!nasm_strnicmp(special, "default", n))
500 sym->other = STV_DEFAULT;
501 else if (!nasm_strnicmp(special, "internal", n))
502 sym->other = STV_INTERNAL;
503 else if (!nasm_strnicmp(special, "hidden", n))
504 sym->other = STV_HIDDEN;
505 else if (!nasm_strnicmp(special, "protected", n))
506 sym->other = STV_PROTECTED;
507 else
508 n = 0;
509 special += n;
512 if (*special) {
513 struct tokenval tokval;
514 expr *e;
515 int fwd = 0;
516 char *saveme = stdscan_get();
518 while (special[n] && nasm_isspace(special[n]))
519 n++;
521 * We have a size expression; attempt to
522 * evaluate it.
524 stdscan_reset();
525 stdscan_set(special + n);
526 tokval.t_type = TOKEN_INVALID;
527 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
528 if (fwd) {
529 sym->nextfwd = fwds;
530 fwds = sym;
531 sym->name = nasm_strdup(name);
532 } else if (e) {
533 if (!is_simple(e))
534 nasm_error(ERR_NONFATAL, "cannot use relocatable"
535 " expression as symbol size");
536 else
537 sym->size = reloc_value(e);
539 stdscan_set(saveme);
541 special_used = true;
544 * If TLS segment, mark symbol accordingly.
546 if (sects[sym->section - 1]->flags & SHF_TLS) {
547 sym->type &= 0xf0;
548 sym->type |= STT_TLS;
551 sym->globnum = nglobs;
552 nglobs++;
553 } else
554 nlocals++;
556 if (special && !special_used)
557 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
560 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
561 int64_t offset, int type)
563 struct elf_reloc *r;
565 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
566 sect->tail = &r->next;
568 r->address = sect->len;
569 r->offset = offset;
571 if (segment != NO_SEG) {
572 int i;
573 for (i = 0; i < nsects; i++)
574 if (segment == sects[i]->index)
575 r->symbol = i + 2;
576 if (!r->symbol)
577 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
579 r->type = type;
581 sect->nrelocs++;
585 * This routine deals with ..got and ..sym relocations: the more
586 * complicated kinds. In shared-library writing, some relocations
587 * with respect to global symbols must refer to the precise symbol
588 * rather than referring to an offset from the base of the section
589 * _containing_ the symbol. Such relocations call to this routine,
590 * which searches the symbol list for the symbol in question.
592 * R_386_GOT32 references require the _exact_ symbol address to be
593 * used; R_386_32 references can be at an offset from the symbol.
594 * The boolean argument `exact' tells us this.
596 * Return value is the adjusted value of `addr', having become an
597 * offset from the symbol rather than the section. Should always be
598 * zero when returning from an exact call.
600 * Limitation: if you define two symbols at the same place,
601 * confusion will occur.
603 * Inefficiency: we search, currently, using a linked list which
604 * isn't even necessarily sorted.
606 static void elf_add_gsym_reloc(struct elf_section *sect,
607 int32_t segment, uint64_t offset, int64_t pcrel,
608 int type, bool exact)
610 struct elf_reloc *r;
611 struct elf_section *s;
612 struct elf_symbol *sym;
613 struct rbtree *srb;
614 int i;
617 * First look up the segment/offset pair and find a global
618 * symbol corresponding to it. If it's not one of our segments,
619 * then it must be an external symbol, in which case we're fine
620 * doing a normal elf_add_reloc after first sanity-checking
621 * that the offset from the symbol is zero.
623 s = NULL;
624 for (i = 0; i < nsects; i++)
625 if (segment == sects[i]->index) {
626 s = sects[i];
627 break;
630 if (!s) {
631 if (exact && offset)
632 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
633 else
634 elf_add_reloc(sect, segment, offset - pcrel, type);
635 return;
638 srb = rb_search(s->gsyms, offset);
639 if (!srb || (exact && srb->key != offset)) {
640 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
641 " for this reference");
642 return;
644 sym = container_of(srb, struct elf_symbol, symv);
646 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
647 sect->tail = &r->next;
648 r->next = NULL;
650 r->address = sect->len;
651 r->offset = offset - pcrel - sym->symv.key;
652 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
653 r->type = type;
655 sect->nrelocs++;
658 static void elf_out(int32_t segto, const void *data,
659 enum out_type type, uint64_t size,
660 int32_t segment, int32_t wrt)
662 struct elf_section *s;
663 int64_t addr;
664 int reltype, bytes;
665 int i;
666 static struct symlininfo sinfo;
668 #if defined(DEBUG) && DEBUG>2
669 if (data)
670 nasm_error(ERR_DEBUG,
671 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
672 currentline, type, segment, segto, size, *(int64_t *)data);
673 else
674 nasm_error(ERR_DEBUG,
675 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
676 currentline, type, segment, segto, size);
677 #endif
680 * handle absolute-assembly (structure definitions)
682 if (segto == NO_SEG) {
683 if (type != OUT_RESERVE)
684 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
685 " space");
686 return;
689 s = NULL;
690 for (i = 0; i < nsects; i++)
691 if (segto == sects[i]->index) {
692 s = sects[i];
693 break;
695 if (!s) {
696 int tempint; /* ignored */
697 if (segto != elf_section_names(".text", 2, &tempint))
698 nasm_panic(0, "strange segment conditions in ELF driver");
699 else {
700 s = sects[nsects - 1];
701 i = nsects - 1;
705 /* again some stabs debugging stuff */
706 sinfo.offset = s->len;
707 sinfo.section = i;
708 sinfo.segto = segto;
709 sinfo.name = s->name;
710 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
711 /* end of debugging stuff */
713 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
714 nasm_error(ERR_WARNING, "attempt to initialize memory in"
715 " BSS section `%s': ignored", s->name);
716 s->len += realsize(type, size);
717 return;
720 switch (type) {
721 case OUT_RESERVE:
722 if (s->type == SHT_PROGBITS) {
723 nasm_error(ERR_WARNING, "uninitialized space declared in"
724 " non-BSS section `%s': zeroing", s->name);
725 elf_sect_write(s, NULL, size);
726 } else
727 s->len += size;
728 break;
730 case OUT_RAWDATA:
731 if (segment != NO_SEG)
732 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
733 elf_sect_write(s, data, size);
734 break;
736 case OUT_ADDRESS:
738 int isize = (int)size;
739 int asize = abs((int)size);
741 addr = *(int64_t *)data;
742 if (segment == NO_SEG) {
743 /* Do nothing */
744 } else if (segment % 2) {
745 nasm_error(ERR_NONFATAL, "ELF format does not support"
746 " segment base references");
747 } else {
748 if (wrt == NO_SEG) {
749 switch (isize) {
750 case 1:
751 case -1:
752 elf_add_reloc(s, segment, addr, R_X86_64_8);
753 break;
754 case 2:
755 case -2:
756 elf_add_reloc(s, segment, addr, R_X86_64_16);
757 break;
758 case 4:
759 elf_add_reloc(s, segment, addr, R_X86_64_32);
760 break;
761 case -4:
762 elf_add_reloc(s, segment, addr, R_X86_64_32S);
763 break;
764 case 8:
765 case -8:
766 elf_add_reloc(s, segment, addr, R_X86_64_64);
767 break;
768 default:
769 nasm_panic(0, "internal error elf64-hpa-871");
770 break;
772 addr = 0;
773 } else if (wrt == elf_gotpc_sect + 1) {
775 * The user will supply GOT relative to $$. ELF
776 * will let us have GOT relative to $. So we
777 * need to fix up the data item by $-$$.
779 addr += s->len;
780 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
781 addr = 0;
782 } else if (wrt == elf_gotoff_sect + 1) {
783 if (asize != 8) {
784 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
785 "references to be qword");
786 } else {
787 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
788 addr = 0;
790 } else if (wrt == elf_got_sect + 1) {
791 switch (asize) {
792 case 4:
793 elf_add_gsym_reloc(s, segment, addr, 0,
794 R_X86_64_GOT32, true);
795 addr = 0;
796 break;
797 case 8:
798 elf_add_gsym_reloc(s, segment, addr, 0,
799 R_X86_64_GOT64, true);
800 addr = 0;
801 break;
802 default:
803 nasm_error(ERR_NONFATAL, "invalid ..got reference");
804 break;
806 } else if (wrt == elf_sym_sect + 1) {
807 switch (isize) {
808 case 1:
809 case -1:
810 elf_add_gsym_reloc(s, segment, addr, 0,
811 R_X86_64_8, false);
812 addr = 0;
813 break;
814 case 2:
815 case -2:
816 elf_add_gsym_reloc(s, segment, addr, 0,
817 R_X86_64_16, false);
818 addr = 0;
819 break;
820 case 4:
821 elf_add_gsym_reloc(s, segment, addr, 0,
822 R_X86_64_32, false);
823 addr = 0;
824 break;
825 case -4:
826 elf_add_gsym_reloc(s, segment, addr, 0,
827 R_X86_64_32S, false);
828 addr = 0;
829 break;
830 case 8:
831 case -8:
832 elf_add_gsym_reloc(s, segment, addr, 0,
833 R_X86_64_64, false);
834 addr = 0;
835 break;
836 default:
837 nasm_panic(0, "internal error elf64-hpa-903");
838 break;
840 } else if (wrt == elf_plt_sect + 1) {
841 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
842 "relative PLT references");
843 } else {
844 nasm_error(ERR_NONFATAL, "ELF format does not support this"
845 " use of WRT");
848 elf_sect_writeaddr(s, addr, asize);
849 break;
852 case OUT_REL1ADR:
853 reltype = R_X86_64_PC8;
854 bytes = 1;
855 goto rel12adr;
857 case OUT_REL2ADR:
858 reltype = R_X86_64_PC16;
859 bytes = 2;
860 goto rel12adr;
862 rel12adr:
863 addr = *(int64_t *)data - size;
864 if (segment == segto)
865 nasm_panic(0, "intra-segment OUT_REL1ADR");
866 if (segment == NO_SEG) {
867 /* Do nothing */
868 } else if (segment % 2) {
869 nasm_error(ERR_NONFATAL, "ELF format does not support"
870 " segment base references");
871 } else {
872 if (wrt == NO_SEG) {
873 elf_add_reloc(s, segment, addr, reltype);
874 addr = 0;
875 } else {
876 nasm_error(ERR_NONFATAL,
877 "Unsupported non-32-bit ELF relocation");
880 elf_sect_writeaddr(s, addr, bytes);
881 break;
883 case OUT_REL4ADR:
884 addr = *(int64_t *)data - size;
885 if (segment == segto)
886 nasm_panic(0, "intra-segment OUT_REL4ADR");
887 if (segment == NO_SEG) {
888 /* Do nothing */
889 } else if (segment % 2) {
890 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
891 " segment base references");
892 } else {
893 if (wrt == NO_SEG) {
894 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
895 addr = 0;
896 } else if (wrt == elf_plt_sect + 1) {
897 elf_add_gsym_reloc(s, segment, addr+size, size,
898 R_X86_64_PLT32, true);
899 addr = 0;
900 } else if (wrt == elf_gotpc_sect + 1 ||
901 wrt == elf_got_sect + 1) {
902 elf_add_gsym_reloc(s, segment, addr+size, size,
903 R_X86_64_GOTPCREL, true);
904 addr = 0;
905 } else if (wrt == elf_gotoff_sect + 1 ||
906 wrt == elf_got_sect + 1) {
907 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
908 "qword absolute");
909 } else if (wrt == elf_gottpoff_sect + 1) {
910 elf_add_gsym_reloc(s, segment, addr+size, size,
911 R_X86_64_GOTTPOFF, true);
912 addr = 0;
913 } else {
914 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
915 " use of WRT");
918 elf_sect_writeaddr(s, addr, 4);
919 break;
921 case OUT_REL8ADR:
922 addr = *(int64_t *)data - size;
923 if (segment == segto)
924 nasm_panic(0, "intra-segment OUT_REL8ADR");
925 if (segment == NO_SEG) {
926 /* Do nothing */
927 } else if (segment % 2) {
928 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
929 " segment base references");
930 } else {
931 if (wrt == NO_SEG) {
932 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
933 addr = 0;
934 } else if (wrt == elf_gotpc_sect + 1 ||
935 wrt == elf_got_sect + 1) {
936 elf_add_gsym_reloc(s, segment, addr+size, size,
937 R_X86_64_GOTPCREL64, true);
938 addr = 0;
939 } else if (wrt == elf_gotoff_sect + 1 ||
940 wrt == elf_got_sect + 1) {
941 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
942 "absolute");
943 } else if (wrt == elf_gottpoff_sect + 1) {
944 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
945 "dword");
946 } else {
947 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
948 " use of WRT");
951 elf_sect_writeaddr(s, addr, 8);
952 break;
956 static void elf_write(void)
958 int align;
959 char *p;
960 int i;
962 struct SAA *symtab;
963 int32_t symtablen, symtablocal;
966 * Work out how many sections we will have. We have SHN_UNDEF,
967 * then the flexible user sections, then the fixed sections
968 * `.shstrtab', `.symtab' and `.strtab', then optionally
969 * relocation sections for the user sections.
971 nsections = sec_numspecial + 1;
972 if (dfmt == &df_stabs)
973 nsections += 3;
974 else if (dfmt == &df_dwarf)
975 nsections += 10;
977 add_sectname("", ".shstrtab");
978 add_sectname("", ".symtab");
979 add_sectname("", ".strtab");
980 for (i = 0; i < nsects; i++) {
981 nsections++; /* for the section itself */
982 if (sects[i]->head) {
983 nsections++; /* for its relocations */
984 add_sectname(".rela", sects[i]->name);
988 if (dfmt == &df_stabs) {
989 /* in case the debug information is wanted, just add these three sections... */
990 add_sectname("", ".stab");
991 add_sectname("", ".stabstr");
992 add_sectname(".rel", ".stab");
995 else if (dfmt == &df_dwarf) {
996 /* the dwarf debug standard specifies the following ten sections,
997 not all of which are currently implemented,
998 although all of them are defined. */
999 #define debug_aranges (int64_t) (nsections-10)
1000 #define debug_info (int64_t) (nsections-7)
1001 #define debug_abbrev (int64_t) (nsections-5)
1002 #define debug_line (int64_t) (nsections-4)
1003 add_sectname("", ".debug_aranges");
1004 add_sectname(".rela", ".debug_aranges");
1005 add_sectname("", ".debug_pubnames");
1006 add_sectname("", ".debug_info");
1007 add_sectname(".rela", ".debug_info");
1008 add_sectname("", ".debug_abbrev");
1009 add_sectname("", ".debug_line");
1010 add_sectname(".rela", ".debug_line");
1011 add_sectname("", ".debug_frame");
1012 add_sectname("", ".debug_loc");
1016 * Output the ELF header.
1018 nasm_write("\177ELF\2\1\1", 7, ofile);
1019 fputc(elf_osabi, ofile);
1020 fputc(elf_abiver, ofile);
1021 fwritezero(7, ofile);
1022 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1023 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1024 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1025 fwriteint64_t(0L, ofile); /* no entry point */
1026 fwriteint64_t(0L, ofile); /* no program header table */
1027 fwriteint64_t(0x40L, ofile); /* section headers straight after
1028 * ELF header plus alignment */
1029 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1030 fwriteint16_t(0x40, ofile); /* size of ELF header */
1031 fwriteint16_t(0, ofile); /* no program header table, again */
1032 fwriteint16_t(0, ofile); /* still no program header table */
1033 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1034 fwriteint16_t(nsections, ofile); /* number of sections */
1035 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1036 * section header table */
1039 * Build the symbol table and relocation tables.
1041 symtab = elf_build_symtab(&symtablen, &symtablocal);
1042 for (i = 0; i < nsects; i++)
1043 if (sects[i]->head)
1044 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1045 sects[i]->head);
1048 * Now output the section header table.
1051 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1052 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1053 elf_foffs += align;
1054 elf_nsect = 0;
1055 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1057 /* SHN_UNDEF */
1058 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1059 p = shstrtab + 1;
1061 /* The normal sections */
1062 for (i = 0; i < nsects; i++) {
1063 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1064 (sects[i]->type == SHT_PROGBITS ?
1065 sects[i]->data : NULL), true,
1066 sects[i]->len, 0, 0, sects[i]->align, 0);
1067 p += strlen(p) + 1;
1070 /* .shstrtab */
1071 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1072 shstrtablen, 0, 0, 1, 0);
1073 p += strlen(p) + 1;
1075 /* .symtab */
1076 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1077 symtablen, sec_strtab, symtablocal, 8, 24);
1078 p += strlen(p) + 1;
1080 /* .strtab */
1081 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1082 strslen, 0, 0, 1, 0);
1083 p += strlen(p) + 1;
1085 /* The relocation sections */
1086 for (i = 0; i < nsects; i++)
1087 if (sects[i]->head) {
1088 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1089 sects[i]->rellen, sec_symtab, i + 1, 8, 24);
1090 p += strlen(p) + 1;
1093 if (dfmt == &df_stabs) {
1094 /* for debugging information, create the last three sections
1095 which are the .stab , .stabstr and .rel.stab sections respectively */
1097 /* this function call creates the stab sections in memory */
1098 stabs64_generate();
1100 if (stabbuf && stabstrbuf && stabrelbuf) {
1101 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1102 stablen, sec_stabstr, 0, 4, 12);
1103 p += strlen(p) + 1;
1105 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1106 stabstrlen, 0, 0, 4, 0);
1107 p += strlen(p) + 1;
1109 /* link -> symtable info -> section to refer to */
1110 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1111 stabrellen, symtabsection, sec_stab, 4, 16);
1112 p += strlen(p) + 1;
1114 } else if (dfmt == &df_dwarf) {
1115 /* for dwarf debugging information, create the ten dwarf sections */
1117 /* this function call creates the dwarf sections in memory */
1118 if (dwarf_fsect)
1119 dwarf64_generate();
1121 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1122 arangeslen, 0, 0, 1, 0);
1123 p += strlen(p) + 1;
1125 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1126 arangesrellen, symtabsection, debug_aranges, 1, 24);
1127 p += strlen(p) + 1;
1129 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1130 pubnameslen, 0, 0, 1, 0);
1131 p += strlen(p) + 1;
1133 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1134 infolen, 0, 0, 1, 0);
1135 p += strlen(p) + 1;
1137 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1138 inforellen, symtabsection, debug_info, 1, 24);
1139 p += strlen(p) + 1;
1141 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1142 abbrevlen, 0, 0, 1, 0);
1143 p += strlen(p) + 1;
1145 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1146 linelen, 0, 0, 1, 0);
1147 p += strlen(p) + 1;
1149 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1150 linerellen, symtabsection, debug_line, 1, 24);
1151 p += strlen(p) + 1;
1153 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1154 framelen, 0, 0, 8, 0);
1155 p += strlen(p) + 1;
1157 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1158 loclen, 0, 0, 1, 0);
1159 p += strlen(p) + 1;
1161 fwritezero(align, ofile);
1164 * Now output the sections.
1166 elf_write_sections();
1168 nasm_free(elf_sects);
1169 saa_free(symtab);
1172 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1174 struct SAA *s = saa_init(1L);
1175 struct elf_symbol *sym;
1176 uint8_t entry[24], *p;
1177 int i;
1179 *len = *local = 0;
1182 * First, an all-zeros entry, required by the ELF spec.
1184 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1185 *len += 24;
1186 (*local)++;
1189 * Next, an entry for the file name.
1191 p = entry;
1192 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1193 WRITESHORT(p, STT_FILE); /* type FILE */
1194 WRITESHORT(p, SHN_ABS);
1195 WRITEDLONG(p, (uint64_t) 0); /* no value */
1196 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1197 saa_wbytes(s, entry, 24L);
1198 *len += 24;
1199 (*local)++;
1202 * Now some standard symbols defining the segments, for relocation
1203 * purposes.
1205 for (i = 1; i <= nsects; i++) {
1206 p = entry;
1207 WRITELONG(p, 0); /* no symbol name */
1208 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1209 WRITESHORT(p, i); /* section id */
1210 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1211 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1212 saa_wbytes(s, entry, 24L);
1213 *len += 24;
1214 (*local)++;
1219 * Now the other local symbols.
1221 saa_rewind(syms);
1222 while ((sym = saa_rstruct(syms))) {
1223 if (sym->type & SYM_GLOBAL)
1224 continue;
1225 p = entry;
1226 WRITELONG(p, sym->strpos); /* index into symbol string table */
1227 WRITECHAR(p, sym->type); /* type and binding */
1228 WRITECHAR(p, sym->other); /* visibility */
1229 WRITESHORT(p, sym->section); /* index into section header table */
1230 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1231 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1232 saa_wbytes(s, entry, 24L);
1233 *len += 24;
1234 (*local)++;
1237 * dwarf needs symbols for debug sections
1238 * which are relocation targets.
1240 if (dfmt == &df_dwarf) {
1241 dwarf_infosym = *local;
1242 p = entry;
1243 WRITELONG(p, 0); /* no symbol name */
1244 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1245 WRITESHORT(p, debug_info); /* section id */
1246 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1247 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1248 saa_wbytes(s, entry, 24L);
1249 *len += 24;
1250 (*local)++;
1251 dwarf_abbrevsym = *local;
1252 p = entry;
1253 WRITELONG(p, 0); /* no symbol name */
1254 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1255 WRITESHORT(p, debug_abbrev); /* section id */
1256 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1257 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1258 saa_wbytes(s, entry, 24L);
1259 *len += 24;
1260 (*local)++;
1261 dwarf_linesym = *local;
1262 p = entry;
1263 WRITELONG(p, 0); /* no symbol name */
1264 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1265 WRITESHORT(p, debug_line); /* section id */
1266 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1267 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1268 saa_wbytes(s, entry, 24L);
1269 *len += 24;
1270 (*local)++;
1274 * Now the global symbols.
1276 saa_rewind(syms);
1277 while ((sym = saa_rstruct(syms))) {
1278 if (!(sym->type & SYM_GLOBAL))
1279 continue;
1280 p = entry;
1281 WRITELONG(p, sym->strpos);
1282 WRITECHAR(p, sym->type); /* type and binding */
1283 WRITECHAR(p, sym->other); /* visibility */
1284 WRITESHORT(p, sym->section);
1285 WRITEDLONG(p, (int64_t)sym->symv.key);
1286 WRITEDLONG(p, (int64_t)sym->size);
1287 saa_wbytes(s, entry, 24L);
1288 *len += 24;
1291 return s;
1294 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1296 struct SAA *s;
1297 uint8_t *p, entry[24];
1298 int32_t global_offset;
1300 if (!r)
1301 return NULL;
1303 s = saa_init(1L);
1304 *len = 0;
1307 * How to onvert from a global placeholder to a real symbol index;
1308 * the +2 refers to the two special entries, the null entry and
1309 * the filename entry.
1311 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1313 while (r) {
1314 int32_t sym = r->symbol;
1316 if (sym >= GLOBAL_TEMP_BASE)
1317 sym += global_offset;
1319 p = entry;
1320 WRITEDLONG(p, r->address);
1321 WRITELONG(p, r->type);
1322 WRITELONG(p, sym);
1323 WRITEDLONG(p, r->offset);
1324 saa_wbytes(s, entry, 24L);
1325 *len += 24;
1327 r = r->next;
1330 return s;
1333 static void elf_section_header(int name, int type, uint64_t flags,
1334 void *data, bool is_saa, uint64_t datalen,
1335 int link, int info, int align, int eltsize)
1337 elf_sects[elf_nsect].data = data;
1338 elf_sects[elf_nsect].len = datalen;
1339 elf_sects[elf_nsect].is_saa = is_saa;
1340 elf_nsect++;
1342 fwriteint32_t((int32_t)name, ofile);
1343 fwriteint32_t((int32_t)type, ofile);
1344 fwriteint64_t((int64_t)flags, ofile);
1345 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1346 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1347 fwriteint64_t(datalen, ofile);
1348 if (data)
1349 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1350 fwriteint32_t((int32_t)link, ofile);
1351 fwriteint32_t((int32_t)info, ofile);
1352 fwriteint64_t((int64_t)align, ofile);
1353 fwriteint64_t((int64_t)eltsize, ofile);
1356 static void elf_write_sections(void)
1358 int i;
1359 for (i = 0; i < elf_nsect; i++)
1360 if (elf_sects[i].data) {
1361 int32_t len = elf_sects[i].len;
1362 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1363 int32_t align = reallen - len;
1364 if (elf_sects[i].is_saa)
1365 saa_fpwrite(elf_sects[i].data, ofile);
1366 else
1367 nasm_write(elf_sects[i].data, len, ofile);
1368 fwritezero(align, ofile);
1372 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
1374 saa_wbytes(sect->data, data, len);
1375 sect->len += len;
1378 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
1380 saa_writeaddr(sect->data, data, len);
1381 sect->len += len;
1384 static void elf_sectalign(int32_t seg, unsigned int value)
1386 struct elf_section *s = NULL;
1387 int i;
1389 for (i = 0; i < nsects; i++) {
1390 if (sects[i]->index == seg) {
1391 s = sects[i];
1392 break;
1395 if (!s || !is_power2(value))
1396 return;
1398 if (value > s->align)
1399 s->align = value;
1402 static int32_t elf_segbase(int32_t segment)
1404 return segment;
1407 static void elf_filename(char *inname, char *outname)
1409 strcpy(elf_module, inname);
1410 standard_extension(inname, outname, ".o");
1413 extern macros_t elf_stdmac[];
1415 static int elf_set_info(enum geninfo type, char **val)
1417 (void)type;
1418 (void)val;
1419 return 0;
1421 static struct dfmt df_dwarf = {
1422 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1423 "dwarf",
1424 dwarf64_init,
1425 dwarf64_linenum,
1426 null_debug_deflabel,
1427 null_debug_directive,
1428 debug64_typevalue,
1429 dwarf64_output,
1430 dwarf64_cleanup
1432 static struct dfmt df_stabs = {
1433 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1434 "stabs",
1435 null_debug_init,
1436 stabs64_linenum,
1437 null_debug_deflabel,
1438 null_debug_directive,
1439 debug64_typevalue,
1440 stabs64_output,
1441 stabs64_cleanup
1444 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1446 struct ofmt of_elf64 = {
1447 "ELF64 (x86_64) object files (e.g. Linux)",
1448 "elf64",
1451 elf64_debugs_arr,
1452 &df_stabs,
1453 elf_stdmac,
1454 elf_init,
1455 elf_set_info,
1456 elf_out,
1457 elf_deflabel,
1458 elf_section_names,
1459 elf_sectalign,
1460 elf_segbase,
1461 elf_directive,
1462 elf_filename,
1463 elf_cleanup
1466 /* common debugging routines */
1467 static void debug64_typevalue(int32_t type)
1469 int32_t stype, ssize;
1470 switch (TYM_TYPE(type)) {
1471 case TY_LABEL:
1472 ssize = 0;
1473 stype = STT_NOTYPE;
1474 break;
1475 case TY_BYTE:
1476 ssize = 1;
1477 stype = STT_OBJECT;
1478 break;
1479 case TY_WORD:
1480 ssize = 2;
1481 stype = STT_OBJECT;
1482 break;
1483 case TY_DWORD:
1484 ssize = 4;
1485 stype = STT_OBJECT;
1486 break;
1487 case TY_FLOAT:
1488 ssize = 4;
1489 stype = STT_OBJECT;
1490 break;
1491 case TY_QWORD:
1492 ssize = 8;
1493 stype = STT_OBJECT;
1494 break;
1495 case TY_TBYTE:
1496 ssize = 10;
1497 stype = STT_OBJECT;
1498 break;
1499 case TY_OWORD:
1500 ssize = 16;
1501 stype = STT_OBJECT;
1502 break;
1503 case TY_YWORD:
1504 ssize = 32;
1505 stype = STT_OBJECT;
1506 break;
1507 case TY_COMMON:
1508 ssize = 0;
1509 stype = STT_COMMON;
1510 break;
1511 case TY_SEG:
1512 ssize = 0;
1513 stype = STT_SECTION;
1514 break;
1515 case TY_EXTERN:
1516 ssize = 0;
1517 stype = STT_NOTYPE;
1518 break;
1519 case TY_EQU:
1520 ssize = 0;
1521 stype = STT_NOTYPE;
1522 break;
1523 default:
1524 ssize = 0;
1525 stype = STT_NOTYPE;
1526 break;
1528 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1529 lastsym->size = ssize;
1530 lastsym->type = stype;
1534 /* stabs debugging routines */
1536 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1538 (void)segto;
1539 if (!stabs_filename) {
1540 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1541 strcpy(stabs_filename, filename);
1542 } else {
1543 if (strcmp(stabs_filename, filename)) {
1544 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1545 in fact, this leak comes in quite handy to maintain a list of files
1546 encountered so far in the symbol lines... */
1548 /* why not nasm_free(stabs_filename); we're done with the old one */
1550 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1551 strcpy(stabs_filename, filename);
1554 debug_immcall = 1;
1555 currentline = linenumber;
1559 static void stabs64_output(int type, void *param)
1561 struct symlininfo *s;
1562 struct linelist *el;
1563 if (type == TY_DEBUGSYMLIN) {
1564 if (debug_immcall) {
1565 s = (struct symlininfo *)param;
1566 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1567 return; /* line info is only collected for executable sections */
1568 numlinestabs++;
1569 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1570 el->info.offset = s->offset;
1571 el->info.section = s->section;
1572 el->info.name = s->name;
1573 el->line = currentline;
1574 el->filename = stabs_filename;
1575 el->next = 0;
1576 if (stabslines) {
1577 stabslines->last->next = el;
1578 stabslines->last = el;
1579 } else {
1580 stabslines = el;
1581 stabslines->last = el;
1585 debug_immcall = 0;
1588 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1590 static void stabs64_generate(void)
1592 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1593 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1594 char **allfiles;
1595 int *fileidx;
1597 struct linelist *ptr;
1599 ptr = stabslines;
1601 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1602 numfiles = 0;
1603 while (ptr) {
1604 if (numfiles == 0) {
1605 allfiles[0] = ptr->filename;
1606 numfiles++;
1607 } else {
1608 for (i = 0; i < numfiles; i++) {
1609 if (!strcmp(allfiles[i], ptr->filename))
1610 break;
1612 if (i >= numfiles) {
1613 allfiles[i] = ptr->filename;
1614 numfiles++;
1617 ptr = ptr->next;
1619 strsize = 1;
1620 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1621 for (i = 0; i < numfiles; i++) {
1622 fileidx[i] = strsize;
1623 strsize += strlen(allfiles[i]) + 1;
1625 mainfileindex = 0;
1626 for (i = 0; i < numfiles; i++) {
1627 if (!strcmp(allfiles[i], elf_module)) {
1628 mainfileindex = i;
1629 break;
1634 * worst case size of the stab buffer would be:
1635 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1636 * plus one "ending" entry
1638 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1639 sizeof(struct stabentry));
1640 ssbuf = (uint8_t *)nasm_malloc(strsize);
1641 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1642 rptr = rbuf;
1644 for (i = 0; i < numfiles; i++)
1645 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1646 ssbuf[0] = 0;
1648 stabstrlen = strsize; /* set global variable for length of stab strings */
1650 sptr = sbuf;
1651 ptr = stabslines;
1652 numstabs = 0;
1654 if (ptr) {
1656 * this is the first stab, its strx points to the filename of the
1657 * the source-file, the n_desc field should be set to the number
1658 * of remaining stabs
1660 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1662 /* this is the stab for the main source file */
1663 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1665 /* relocation table entry */
1668 * Since the symbol table has two entries before
1669 * the section symbols, the index in the info.section
1670 * member must be adjusted by adding 2
1673 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1674 WRITELONG(rptr, R_X86_64_32);
1675 WRITELONG(rptr, ptr->info.section + 2);
1677 numstabs++;
1678 currfile = mainfileindex;
1681 while (ptr) {
1682 if (strcmp(allfiles[currfile], ptr->filename)) {
1683 /* oops file has changed... */
1684 for (i = 0; i < numfiles; i++)
1685 if (!strcmp(allfiles[i], ptr->filename))
1686 break;
1687 currfile = i;
1688 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1689 ptr->info.offset);
1690 numstabs++;
1692 /* relocation table entry */
1694 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1695 WRITELONG(rptr, R_X86_64_32);
1696 WRITELONG(rptr, ptr->info.section + 2);
1699 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1700 numstabs++;
1702 /* relocation table entry */
1704 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1705 WRITELONG(rptr, R_X86_64_32);
1706 WRITELONG(rptr, ptr->info.section + 2);
1708 ptr = ptr->next;
1712 /* this is an "ending" token */
1713 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1714 numstabs++;
1716 ((struct stabentry *)sbuf)->n_desc = numstabs;
1718 nasm_free(allfiles);
1719 nasm_free(fileidx);
1721 stablen = (sptr - sbuf);
1722 stabrellen = (rptr - rbuf);
1723 stabrelbuf = rbuf;
1724 stabbuf = sbuf;
1725 stabstrbuf = ssbuf;
1728 static void stabs64_cleanup(void)
1730 struct linelist *ptr, *del;
1731 if (!stabslines)
1732 return;
1734 ptr = stabslines;
1735 while (ptr) {
1736 del = ptr;
1737 ptr = ptr->next;
1738 nasm_free(del);
1741 nasm_free(stabbuf);
1742 nasm_free(stabrelbuf);
1743 nasm_free(stabstrbuf);
1746 /* dwarf routines */
1748 static void dwarf64_init(void)
1750 ndebugs = 3; /* 3 debug symbols */
1753 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1754 int32_t segto)
1756 (void)segto;
1757 dwarf64_findfile(filename);
1758 debug_immcall = 1;
1759 currentline = linenumber;
1762 /* called from elf_out with type == TY_DEBUGSYMLIN */
1763 static void dwarf64_output(int type, void *param)
1765 int ln, aa, inx, maxln, soc;
1766 struct symlininfo *s;
1767 struct SAA *plinep;
1769 (void)type;
1771 s = (struct symlininfo *)param;
1773 /* line number info is only gathered for executable sections */
1774 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1775 return;
1777 /* Check if section index has changed */
1778 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1779 dwarf64_findsect(s->section);
1781 /* do nothing unless line or file has changed */
1782 if (!debug_immcall)
1783 return;
1785 ln = currentline - dwarf_csect->line;
1786 aa = s->offset - dwarf_csect->offset;
1787 inx = dwarf_clist->line;
1788 plinep = dwarf_csect->psaa;
1789 /* check for file change */
1790 if (!(inx == dwarf_csect->file)) {
1791 saa_write8(plinep,DW_LNS_set_file);
1792 saa_write8(plinep,inx);
1793 dwarf_csect->file = inx;
1795 /* check for line change */
1796 if (ln) {
1797 /* test if in range of special op code */
1798 maxln = line_base + line_range;
1799 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1800 if (ln >= line_base && ln < maxln && soc < 256) {
1801 saa_write8(plinep,soc);
1802 } else {
1803 saa_write8(plinep,DW_LNS_advance_line);
1804 saa_wleb128s(plinep,ln);
1805 if (aa) {
1806 saa_write8(plinep,DW_LNS_advance_pc);
1807 saa_wleb128u(plinep,aa);
1810 dwarf_csect->line = currentline;
1811 dwarf_csect->offset = s->offset;
1814 /* show change handled */
1815 debug_immcall = 0;
1819 static void dwarf64_generate(void)
1821 uint8_t *pbuf;
1822 int indx;
1823 struct linelist *ftentry;
1824 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1825 struct SAA *parangesrel, *plinesrel, *pinforel;
1826 struct sectlist *psect;
1827 size_t saalen, linepoff, totlen, highaddr;
1829 /* write epilogues for each line program range */
1830 /* and build aranges section */
1831 paranges = saa_init(1L);
1832 parangesrel = saa_init(1L);
1833 saa_write16(paranges,3); /* dwarf version */
1834 saa_write64(parangesrel, paranges->datalen+4);
1835 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1836 saa_write64(parangesrel, 0);
1837 saa_write32(paranges,0); /* offset into info */
1838 saa_write8(paranges,8); /* pointer size */
1839 saa_write8(paranges,0); /* not segmented */
1840 saa_write32(paranges,0); /* padding */
1841 /* iterate though sectlist entries */
1842 psect = dwarf_fsect;
1843 totlen = 0;
1844 highaddr = 0;
1845 for (indx = 0; indx < dwarf_nsections; indx++)
1847 plinep = psect->psaa;
1848 /* Line Number Program Epilogue */
1849 saa_write8(plinep,2); /* std op 2 */
1850 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1851 saa_write8(plinep,DW_LNS_extended_op);
1852 saa_write8(plinep,1); /* operand length */
1853 saa_write8(plinep,DW_LNE_end_sequence);
1854 totlen += plinep->datalen;
1855 /* range table relocation entry */
1856 saa_write64(parangesrel, paranges->datalen + 4);
1857 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1858 saa_write64(parangesrel, (uint64_t) 0);
1859 /* range table entry */
1860 saa_write64(paranges,0x0000); /* range start */
1861 saa_write64(paranges,sects[psect->section]->len); /* range length */
1862 highaddr += sects[psect->section]->len;
1863 /* done with this entry */
1864 psect = psect->next;
1866 saa_write64(paranges,0); /* null address */
1867 saa_write64(paranges,0); /* null length */
1868 saalen = paranges->datalen;
1869 arangeslen = saalen + 4;
1870 arangesbuf = pbuf = nasm_malloc(arangeslen);
1871 WRITELONG(pbuf,saalen); /* initial length */
1872 saa_rnbytes(paranges, pbuf, saalen);
1873 saa_free(paranges);
1875 /* build rela.aranges section */
1876 arangesrellen = saalen = parangesrel->datalen;
1877 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1878 saa_rnbytes(parangesrel, pbuf, saalen);
1879 saa_free(parangesrel);
1881 /* build pubnames section */
1882 ppubnames = saa_init(1L);
1883 saa_write16(ppubnames,3); /* dwarf version */
1884 saa_write32(ppubnames,0); /* offset into info */
1885 saa_write32(ppubnames,0); /* space used in info */
1886 saa_write32(ppubnames,0); /* end of list */
1887 saalen = ppubnames->datalen;
1888 pubnameslen = saalen + 4;
1889 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1890 WRITELONG(pbuf,saalen); /* initial length */
1891 saa_rnbytes(ppubnames, pbuf, saalen);
1892 saa_free(ppubnames);
1894 /* build info section */
1895 pinfo = saa_init(1L);
1896 pinforel = saa_init(1L);
1897 saa_write16(pinfo,3); /* dwarf version */
1898 saa_write64(pinforel, pinfo->datalen + 4);
1899 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
1900 saa_write64(pinforel, 0);
1901 saa_write32(pinfo,0); /* offset into abbrev */
1902 saa_write8(pinfo,8); /* pointer size */
1903 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1904 saa_write64(pinforel, pinfo->datalen + 4);
1905 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1906 saa_write64(pinforel, 0);
1907 saa_write64(pinfo,0); /* DW_AT_low_pc */
1908 saa_write64(pinforel, pinfo->datalen + 4);
1909 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1910 saa_write64(pinforel, 0);
1911 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
1912 saa_write64(pinforel, pinfo->datalen + 4);
1913 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
1914 saa_write64(pinforel, 0);
1915 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1916 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1917 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1918 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1919 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1920 saa_write64(pinforel, pinfo->datalen + 4);
1921 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1922 saa_write64(pinforel, 0);
1923 saa_write64(pinfo,0); /* DW_AT_low_pc */
1924 saa_write64(pinfo,0); /* DW_AT_frame_base */
1925 saa_write8(pinfo,0); /* end of entries */
1926 saalen = pinfo->datalen;
1927 infolen = saalen + 4;
1928 infobuf = pbuf = nasm_malloc(infolen);
1929 WRITELONG(pbuf,saalen); /* initial length */
1930 saa_rnbytes(pinfo, pbuf, saalen);
1931 saa_free(pinfo);
1933 /* build rela.info section */
1934 inforellen = saalen = pinforel->datalen;
1935 inforelbuf = pbuf = nasm_malloc(inforellen);
1936 saa_rnbytes(pinforel, pbuf, saalen);
1937 saa_free(pinforel);
1939 /* build abbrev section */
1940 pabbrev = saa_init(1L);
1941 saa_write8(pabbrev,1); /* entry number LEB128u */
1942 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1943 saa_write8(pabbrev,1); /* has children */
1944 /* the following attributes and forms are all LEB128u values */
1945 saa_write8(pabbrev,DW_AT_low_pc);
1946 saa_write8(pabbrev,DW_FORM_addr);
1947 saa_write8(pabbrev,DW_AT_high_pc);
1948 saa_write8(pabbrev,DW_FORM_addr);
1949 saa_write8(pabbrev,DW_AT_stmt_list);
1950 saa_write8(pabbrev,DW_FORM_data4);
1951 saa_write8(pabbrev,DW_AT_name);
1952 saa_write8(pabbrev,DW_FORM_string);
1953 saa_write8(pabbrev,DW_AT_producer);
1954 saa_write8(pabbrev,DW_FORM_string);
1955 saa_write8(pabbrev,DW_AT_language);
1956 saa_write8(pabbrev,DW_FORM_data2);
1957 saa_write16(pabbrev,0); /* end of entry */
1958 /* LEB128u usage same as above */
1959 saa_write8(pabbrev,2); /* entry number */
1960 saa_write8(pabbrev,DW_TAG_subprogram);
1961 saa_write8(pabbrev,0); /* no children */
1962 saa_write8(pabbrev,DW_AT_low_pc);
1963 saa_write8(pabbrev,DW_FORM_addr);
1964 saa_write8(pabbrev,DW_AT_frame_base);
1965 saa_write8(pabbrev,DW_FORM_data4);
1966 saa_write16(pabbrev,0); /* end of entry */
1967 abbrevlen = saalen = pabbrev->datalen;
1968 abbrevbuf = pbuf = nasm_malloc(saalen);
1969 saa_rnbytes(pabbrev, pbuf, saalen);
1970 saa_free(pabbrev);
1972 /* build line section */
1973 /* prolog */
1974 plines = saa_init(1L);
1975 saa_write8(plines,1); /* Minimum Instruction Length */
1976 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1977 saa_write8(plines,line_base); /* Line Base */
1978 saa_write8(plines,line_range); /* Line Range */
1979 saa_write8(plines,opcode_base); /* Opcode Base */
1980 /* standard opcode lengths (# of LEB128u operands) */
1981 saa_write8(plines,0); /* Std opcode 1 length */
1982 saa_write8(plines,1); /* Std opcode 2 length */
1983 saa_write8(plines,1); /* Std opcode 3 length */
1984 saa_write8(plines,1); /* Std opcode 4 length */
1985 saa_write8(plines,1); /* Std opcode 5 length */
1986 saa_write8(plines,0); /* Std opcode 6 length */
1987 saa_write8(plines,0); /* Std opcode 7 length */
1988 saa_write8(plines,0); /* Std opcode 8 length */
1989 saa_write8(plines,1); /* Std opcode 9 length */
1990 saa_write8(plines,0); /* Std opcode 10 length */
1991 saa_write8(plines,0); /* Std opcode 11 length */
1992 saa_write8(plines,1); /* Std opcode 12 length */
1993 /* Directory Table */
1994 saa_write8(plines,0); /* End of table */
1995 /* File Name Table */
1996 ftentry = dwarf_flist;
1997 for (indx = 0;indx<dwarf_numfiles;indx++)
1999 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2000 saa_write8(plines,0); /* directory LEB128u */
2001 saa_write8(plines,0); /* time LEB128u */
2002 saa_write8(plines,0); /* size LEB128u */
2003 ftentry = ftentry->next;
2005 saa_write8(plines,0); /* End of table */
2006 linepoff = plines->datalen;
2007 linelen = linepoff + totlen + 10;
2008 linebuf = pbuf = nasm_malloc(linelen);
2009 WRITELONG(pbuf,linelen-4); /* initial length */
2010 WRITESHORT(pbuf,3); /* dwarf version */
2011 WRITELONG(pbuf,linepoff); /* offset to line number program */
2012 /* write line header */
2013 saalen = linepoff;
2014 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2015 pbuf += linepoff;
2016 saa_free(plines);
2017 /* concatonate line program ranges */
2018 linepoff += 13;
2019 plinesrel = saa_init(1L);
2020 psect = dwarf_fsect;
2021 for (indx = 0; indx < dwarf_nsections; indx++) {
2022 saa_write64(plinesrel, linepoff);
2023 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2024 saa_write64(plinesrel, (uint64_t) 0);
2025 plinep = psect->psaa;
2026 saalen = plinep->datalen;
2027 saa_rnbytes(plinep, pbuf, saalen);
2028 pbuf += saalen;
2029 linepoff += saalen;
2030 saa_free(plinep);
2031 /* done with this entry */
2032 psect = psect->next;
2036 /* build rela.lines section */
2037 linerellen =saalen = plinesrel->datalen;
2038 linerelbuf = pbuf = nasm_malloc(linerellen);
2039 saa_rnbytes(plinesrel, pbuf, saalen);
2040 saa_free(plinesrel);
2042 /* build frame section */
2043 framelen = 4;
2044 framebuf = pbuf = nasm_malloc(framelen);
2045 WRITELONG(pbuf,framelen-4); /* initial length */
2047 /* build loc section */
2048 loclen = 16;
2049 locbuf = pbuf = nasm_malloc(loclen);
2050 WRITEDLONG(pbuf,0); /* null beginning offset */
2051 WRITEDLONG(pbuf,0); /* null ending offset */
2054 static void dwarf64_cleanup(void)
2056 nasm_free(arangesbuf);
2057 nasm_free(arangesrelbuf);
2058 nasm_free(pubnamesbuf);
2059 nasm_free(infobuf);
2060 nasm_free(inforelbuf);
2061 nasm_free(abbrevbuf);
2062 nasm_free(linebuf);
2063 nasm_free(linerelbuf);
2064 nasm_free(framebuf);
2065 nasm_free(locbuf);
2068 static void dwarf64_findfile(const char * fname)
2070 int finx;
2071 struct linelist *match;
2073 /* return if fname is current file name */
2074 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2075 return;
2077 /* search for match */
2078 match = 0;
2079 if (dwarf_flist) {
2080 match = dwarf_flist;
2081 for (finx = 0; finx < dwarf_numfiles; finx++) {
2082 if (!(strcmp(fname, match->filename))) {
2083 dwarf_clist = match;
2084 return;
2089 /* add file name to end of list */
2090 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2091 dwarf_numfiles++;
2092 dwarf_clist->line = dwarf_numfiles;
2093 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2094 strcpy(dwarf_clist->filename,fname);
2095 dwarf_clist->next = 0;
2096 if (!dwarf_flist) { /* if first entry */
2097 dwarf_flist = dwarf_elist = dwarf_clist;
2098 dwarf_clist->last = 0;
2099 } else { /* chain to previous entry */
2100 dwarf_elist->next = dwarf_clist;
2101 dwarf_elist = dwarf_clist;
2105 static void dwarf64_findsect(const int index)
2107 int sinx;
2108 struct sectlist *match;
2109 struct SAA *plinep;
2111 /* return if index is current section index */
2112 if (dwarf_csect && (dwarf_csect->section == index))
2113 return;
2115 /* search for match */
2116 match = 0;
2117 if (dwarf_fsect) {
2118 match = dwarf_fsect;
2119 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2120 if (match->section == index) {
2121 dwarf_csect = match;
2122 return;
2124 match = match->next;
2128 /* add entry to end of list */
2129 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2130 dwarf_nsections++;
2131 dwarf_csect->psaa = plinep = saa_init(1L);
2132 dwarf_csect->line = 1;
2133 dwarf_csect->offset = 0;
2134 dwarf_csect->file = 1;
2135 dwarf_csect->section = index;
2136 dwarf_csect->next = 0;
2137 /* set relocatable address at start of line program */
2138 saa_write8(plinep,DW_LNS_extended_op);
2139 saa_write8(plinep,9); /* operand length */
2140 saa_write8(plinep,DW_LNE_set_address);
2141 saa_write64(plinep,0); /* Start Address */
2143 if (!dwarf_fsect) { /* if first entry */
2144 dwarf_fsect = dwarf_esect = dwarf_csect;
2145 dwarf_csect->last = 0;
2146 } else { /* chain to previous entry */
2147 dwarf_esect->next = dwarf_csect;
2148 dwarf_esect = dwarf_csect;
2152 #endif /* OF_ELF */