Change rights 644 to 755
[nasm/github.git] / output / outelf64.c
blob19bd8459cba1fdd829d3cb57bea481eacb30f40f
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 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);
164 static void debug64_deflabel(char *, int32_t, int64_t, int, char *);
165 static void debug64_directive(const char *, const char *);
167 /* stabs debugging routines */
168 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
169 static void stabs64_output(int, void *);
170 static void stabs64_generate(void);
171 static void stabs64_cleanup(void);
173 /* dwarf debugging routines */
174 static void dwarf64_init(void);
175 static void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t);
176 static void dwarf64_output(int, void *);
177 static void dwarf64_generate(void);
178 static void dwarf64_cleanup(void);
179 static void dwarf64_findfile(const char *);
180 static void dwarf64_findsect(const int);
183 * Special section numbers which are used to define ELF special
184 * symbols, which can be used with WRT to provide PIC relocation
185 * types.
187 static int32_t elf_gotpc_sect, elf_gotoff_sect;
188 static int32_t elf_got_sect, elf_plt_sect;
189 static int32_t elf_sym_sect;
190 static int32_t elf_gottpoff_sect;
192 static void elf_init(void)
194 maxbits = 64;
195 sects = NULL;
196 nsects = sectlen = 0;
197 syms = saa_init((int32_t)sizeof(struct elf_symbol));
198 nlocals = nglobs = ndebugs = 0;
199 bsym = raa_init();
200 strs = saa_init(1L);
201 saa_wbytes(strs, "\0", 1L);
202 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
203 strslen = 2 + strlen(elf_module);
204 shstrtab = NULL;
205 shstrtablen = shstrtabsize = 0;;
206 add_sectname("", "");
208 fwds = NULL;
210 elf_gotpc_sect = seg_alloc();
211 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
212 elf_gotoff_sect = seg_alloc();
213 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
214 elf_got_sect = seg_alloc();
215 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
216 elf_plt_sect = seg_alloc();
217 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
218 elf_sym_sect = seg_alloc();
219 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
220 elf_gottpoff_sect = seg_alloc();
221 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
223 def_seg = seg_alloc();
227 static void elf_cleanup(int debuginfo)
229 struct elf_reloc *r;
230 int i;
232 (void)debuginfo;
234 elf_write();
235 for (i = 0; i < nsects; i++) {
236 if (sects[i]->type != SHT_NOBITS)
237 saa_free(sects[i]->data);
238 if (sects[i]->head)
239 saa_free(sects[i]->rel);
240 while (sects[i]->head) {
241 r = sects[i]->head;
242 sects[i]->head = sects[i]->head->next;
243 nasm_free(r);
246 nasm_free(sects);
247 saa_free(syms);
248 raa_free(bsym);
249 saa_free(strs);
250 if (of_elf64.current_dfmt) {
251 of_elf64.current_dfmt->cleanup();
255 /* add entry to the elf .shstrtab section */
256 static void add_sectname(char *firsthalf, char *secondhalf)
258 int len = strlen(firsthalf) + strlen(secondhalf);
259 while (shstrtablen + len + 1 > shstrtabsize)
260 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
261 strcpy(shstrtab + shstrtablen, firsthalf);
262 strcat(shstrtab + shstrtablen, secondhalf);
263 shstrtablen += len + 1;
266 static int elf_make_section(char *name, int type, int flags, int align)
268 struct elf_section *s;
270 s = nasm_zalloc(sizeof(*s));
272 if (type != SHT_NOBITS)
273 s->data = saa_init(1L);
274 s->tail = &s->head;
275 if (!strcmp(name, ".text"))
276 s->index = def_seg;
277 else
278 s->index = seg_alloc();
279 add_sectname("", name);
281 s->name = nasm_strdup(name);
282 s->type = type;
283 s->flags = flags;
284 s->align = align;
286 if (nsects >= sectlen)
287 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
288 sects[nsects++] = s;
290 return nsects - 1;
293 static int32_t elf_section_names(char *name, int pass, int *bits)
295 char *p;
296 uint32_t flags, flags_and, flags_or;
297 uint64_t align;
298 int type, i;
301 * Default is 64 bits.
303 if (!name) {
304 *bits = 64;
305 return def_seg;
308 p = nasm_skip_word(name);
309 if (*p)
310 *p++ = '\0';
311 flags_and = flags_or = type = align = 0;
313 elf_section_attrib(name, p, pass, &flags_and,
314 &flags_or, &align, &type);
316 if (!strcmp(name, ".shstrtab") ||
317 !strcmp(name, ".symtab") ||
318 !strcmp(name, ".strtab")) {
319 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
320 "name `%s'", name);
321 return NO_SEG;
324 for (i = 0; i < nsects; i++)
325 if (!strcmp(name, sects[i]->name))
326 break;
327 if (i == nsects) {
328 const struct elf_known_section *ks = elf_known_sections;
330 while (ks->name) {
331 if (!strcmp(name, ks->name))
332 break;
333 ks++;
336 type = type ? type : ks->type;
337 align = align ? align : ks->align;
338 flags = (ks->flags & ~flags_and) | flags_or;
340 i = elf_make_section(name, type, flags, align);
341 } else if (pass == 1) {
342 if ((type && sects[i]->type != type)
343 || (align && sects[i]->align != align)
344 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
345 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
346 " redeclaration of section `%s'", name);
349 return sects[i]->index;
352 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
353 int is_global, char *special)
355 int pos = strslen;
356 struct elf_symbol *sym;
357 bool special_used = false;
359 #if defined(DEBUG) && DEBUG>2
360 nasm_error(ERR_DEBUG,
361 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
362 name, segment, offset, is_global, special);
363 #endif
364 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
366 * This is a NASM special symbol. We never allow it into
367 * the ELF symbol table, even if it's a valid one. If it
368 * _isn't_ a valid one, we should barf immediately.
370 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
371 strcmp(name, "..got") && strcmp(name, "..plt") &&
372 strcmp(name, "..sym") && strcmp(name, "..gottpoff"))
373 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
374 return;
377 if (is_global == 3) {
378 struct elf_symbol **s;
380 * Fix up a forward-reference symbol size from the first
381 * pass.
383 for (s = &fwds; *s; s = &(*s)->nextfwd)
384 if (!strcmp((*s)->name, name)) {
385 struct tokenval tokval;
386 expr *e;
387 char *p = nasm_skip_spaces(nasm_skip_word(special));
389 stdscan_reset();
390 stdscan_set(p);
391 tokval.t_type = TOKEN_INVALID;
392 e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
393 if (e) {
394 if (!is_simple(e))
395 nasm_error(ERR_NONFATAL, "cannot use relocatable"
396 " expression as symbol size");
397 else
398 (*s)->size = reloc_value(e);
402 * Remove it from the list of unresolved sizes.
404 nasm_free((*s)->name);
405 *s = (*s)->nextfwd;
406 return;
408 return; /* it wasn't an important one */
411 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
412 strslen += 1 + strlen(name);
414 lastsym = sym = saa_wstruct(syms);
416 memset(&sym->symv, 0, sizeof(struct rbtree));
418 sym->strpos = pos;
419 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
420 sym->other = STV_DEFAULT;
421 sym->size = 0;
422 if (segment == NO_SEG)
423 sym->section = SHN_ABS;
424 else {
425 int i;
426 sym->section = SHN_UNDEF;
427 if (segment == def_seg) {
428 /* we have to be sure at least text section is there */
429 int tempint;
430 if (segment != elf_section_names(".text", 2, &tempint))
431 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
433 for (i = 0; i < nsects; i++) {
434 if (segment == sects[i]->index) {
435 sym->section = i + 1;
436 break;
441 if (is_global == 2) {
442 sym->size = offset;
443 sym->symv.key = 0;
444 sym->section = SHN_COMMON;
446 * We have a common variable. Check the special text to see
447 * if it's a valid number and power of two; if so, store it
448 * as the alignment for the common variable.
450 if (special) {
451 bool err;
452 sym->symv.key = readnum(special, &err);
453 if (err)
454 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
455 " valid number", special);
456 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
457 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
458 " power of two", special);
460 special_used = true;
461 } else
462 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
464 if (sym->type == SYM_GLOBAL) {
466 * If sym->section == SHN_ABS, then the first line of the
467 * else section would cause a core dump, because its a reference
468 * beyond the end of the section array.
469 * This behaviour is exhibited by this code:
470 * GLOBAL crash_nasm
471 * crash_nasm equ 0
472 * To avoid such a crash, such requests are silently discarded.
473 * This may not be the best solution.
475 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
476 bsym = raa_write(bsym, segment, nglobs);
477 } else if (sym->section != SHN_ABS) {
479 * This is a global symbol; so we must add it to the rbtree
480 * of global symbols in its section.
482 * In addition, we check the special text for symbol
483 * type and size information.
485 sects[sym->section-1]->gsyms =
486 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
488 if (special) {
489 int n = strcspn(special, " \t");
491 if (!nasm_strnicmp(special, "function", n))
492 sym->type |= STT_FUNC;
493 else if (!nasm_strnicmp(special, "data", n) ||
494 !nasm_strnicmp(special, "object", n))
495 sym->type |= STT_OBJECT;
496 else if (!nasm_strnicmp(special, "notype", n))
497 sym->type |= STT_NOTYPE;
498 else
499 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
500 n, special);
501 special += n;
503 special = nasm_skip_spaces(special);
504 if (*special) {
505 n = strcspn(special, " \t");
506 if (!nasm_strnicmp(special, "default", n))
507 sym->other = STV_DEFAULT;
508 else if (!nasm_strnicmp(special, "internal", n))
509 sym->other = STV_INTERNAL;
510 else if (!nasm_strnicmp(special, "hidden", n))
511 sym->other = STV_HIDDEN;
512 else if (!nasm_strnicmp(special, "protected", n))
513 sym->other = STV_PROTECTED;
514 else
515 n = 0;
516 special += n;
519 if (*special) {
520 struct tokenval tokval;
521 expr *e;
522 int fwd = 0;
523 char *saveme = stdscan_get();
525 while (special[n] && nasm_isspace(special[n]))
526 n++;
528 * We have a size expression; attempt to
529 * evaluate it.
531 stdscan_reset();
532 stdscan_set(special + n);
533 tokval.t_type = TOKEN_INVALID;
534 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
535 NULL);
536 if (fwd) {
537 sym->nextfwd = fwds;
538 fwds = sym;
539 sym->name = nasm_strdup(name);
540 } else if (e) {
541 if (!is_simple(e))
542 nasm_error(ERR_NONFATAL, "cannot use relocatable"
543 " expression as symbol size");
544 else
545 sym->size = reloc_value(e);
547 stdscan_set(saveme);
549 special_used = true;
552 * If TLS segment, mark symbol accordingly.
554 if (sects[sym->section - 1]->flags & SHF_TLS) {
555 sym->type &= 0xf0;
556 sym->type |= STT_TLS;
559 sym->globnum = nglobs;
560 nglobs++;
561 } else
562 nlocals++;
564 if (special && !special_used)
565 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
568 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
569 int64_t offset, int type)
571 struct elf_reloc *r;
573 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
574 sect->tail = &r->next;
576 r->address = sect->len;
577 r->offset = offset;
579 if (segment != NO_SEG) {
580 int i;
581 for (i = 0; i < nsects; i++)
582 if (segment == sects[i]->index)
583 r->symbol = i + 2;
584 if (!r->symbol)
585 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
587 r->type = type;
589 sect->nrelocs++;
593 * This routine deals with ..got and ..sym relocations: the more
594 * complicated kinds. In shared-library writing, some relocations
595 * with respect to global symbols must refer to the precise symbol
596 * rather than referring to an offset from the base of the section
597 * _containing_ the symbol. Such relocations call to this routine,
598 * which searches the symbol list for the symbol in question.
600 * R_386_GOT32 references require the _exact_ symbol address to be
601 * used; R_386_32 references can be at an offset from the symbol.
602 * The boolean argument `exact' tells us this.
604 * Return value is the adjusted value of `addr', having become an
605 * offset from the symbol rather than the section. Should always be
606 * zero when returning from an exact call.
608 * Limitation: if you define two symbols at the same place,
609 * confusion will occur.
611 * Inefficiency: we search, currently, using a linked list which
612 * isn't even necessarily sorted.
614 static void elf_add_gsym_reloc(struct elf_section *sect,
615 int32_t segment, uint64_t offset, int64_t pcrel,
616 int type, bool exact)
618 struct elf_reloc *r;
619 struct elf_section *s;
620 struct elf_symbol *sym;
621 struct rbtree *srb;
622 int i;
625 * First look up the segment/offset pair and find a global
626 * symbol corresponding to it. If it's not one of our segments,
627 * then it must be an external symbol, in which case we're fine
628 * doing a normal elf_add_reloc after first sanity-checking
629 * that the offset from the symbol is zero.
631 s = NULL;
632 for (i = 0; i < nsects; i++)
633 if (segment == sects[i]->index) {
634 s = sects[i];
635 break;
638 if (!s) {
639 if (exact && offset)
640 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
641 else
642 elf_add_reloc(sect, segment, offset - pcrel, type);
643 return;
646 srb = rb_search(s->gsyms, offset);
647 if (!srb || (exact && srb->key != offset)) {
648 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
649 " for this reference");
650 return;
652 sym = container_of(srb, struct elf_symbol, symv);
654 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
655 sect->tail = &r->next;
656 r->next = NULL;
658 r->address = sect->len;
659 r->offset = offset - pcrel - sym->symv.key;
660 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
661 r->type = type;
663 sect->nrelocs++;
666 static void elf_out(int32_t segto, const void *data,
667 enum out_type type, uint64_t size,
668 int32_t segment, int32_t wrt)
670 struct elf_section *s;
671 int64_t addr;
672 int reltype, bytes;
673 int i;
674 static struct symlininfo sinfo;
676 #if defined(DEBUG) && DEBUG>2
677 if (data)
678 nasm_error(ERR_DEBUG,
679 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
680 currentline, type, segment, segto, size, *(int64_t *)data);
681 else
682 nasm_error(ERR_DEBUG,
683 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
684 currentline, type, segment, segto, size);
685 #endif
688 * handle absolute-assembly (structure definitions)
690 if (segto == NO_SEG) {
691 if (type != OUT_RESERVE)
692 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
693 " space");
694 return;
697 s = NULL;
698 for (i = 0; i < nsects; i++)
699 if (segto == sects[i]->index) {
700 s = sects[i];
701 break;
703 if (!s) {
704 int tempint; /* ignored */
705 if (segto != elf_section_names(".text", 2, &tempint))
706 nasm_error(ERR_PANIC, "strange segment conditions in ELF driver");
707 else {
708 s = sects[nsects - 1];
709 i = nsects - 1;
713 /* again some stabs debugging stuff */
714 if (of_elf64.current_dfmt) {
715 sinfo.offset = s->len;
716 sinfo.section = i;
717 sinfo.segto = segto;
718 sinfo.name = s->name;
719 of_elf64.current_dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
721 /* end of debugging stuff */
723 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
724 nasm_error(ERR_WARNING, "attempt to initialize memory in"
725 " BSS section `%s': ignored", s->name);
726 s->len += realsize(type, size);
727 return;
730 switch (type) {
731 case OUT_RESERVE:
732 if (s->type == SHT_PROGBITS) {
733 nasm_error(ERR_WARNING, "uninitialized space declared in"
734 " non-BSS section `%s': zeroing", s->name);
735 elf_sect_write(s, NULL, size);
736 } else
737 s->len += size;
738 break;
740 case OUT_RAWDATA:
741 if (segment != NO_SEG)
742 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
743 elf_sect_write(s, data, size);
744 break;
746 case OUT_ADDRESS:
748 int isize = (int)size;
749 int asize = abs(size);
751 addr = *(int64_t *)data;
752 if (segment == NO_SEG) {
753 /* Do nothing */
754 } else if (segment % 2) {
755 nasm_error(ERR_NONFATAL, "ELF format does not support"
756 " segment base references");
757 } else {
758 if (wrt == NO_SEG) {
759 switch (isize) {
760 case 1:
761 case -1:
762 elf_add_reloc(s, segment, addr, R_X86_64_8);
763 break;
764 case 2:
765 case -2:
766 elf_add_reloc(s, segment, addr, R_X86_64_16);
767 break;
768 case 4:
769 elf_add_reloc(s, segment, addr, R_X86_64_32);
770 break;
771 case -4:
772 elf_add_reloc(s, segment, addr, R_X86_64_32S);
773 break;
774 case 8:
775 case -8:
776 elf_add_reloc(s, segment, addr, R_X86_64_64);
777 break;
778 default:
779 nasm_error(ERR_PANIC, "internal error elf64-hpa-871");
780 break;
782 addr = 0;
783 } else if (wrt == elf_gotpc_sect + 1) {
785 * The user will supply GOT relative to $$. ELF
786 * will let us have GOT relative to $. So we
787 * need to fix up the data item by $-$$.
789 addr += s->len;
790 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
791 addr = 0;
792 } else if (wrt == elf_gotoff_sect + 1) {
793 if (asize != 8) {
794 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff "
795 "references to be qword");
796 } else {
797 elf_add_reloc(s, segment, addr, R_X86_64_GOTOFF64);
798 addr = 0;
800 } else if (wrt == elf_got_sect + 1) {
801 switch (asize) {
802 case 4:
803 elf_add_gsym_reloc(s, segment, addr, 0,
804 R_X86_64_GOT32, true);
805 addr = 0;
806 break;
807 case 8:
808 elf_add_gsym_reloc(s, segment, addr, 0,
809 R_X86_64_GOT64, true);
810 addr = 0;
811 break;
812 default:
813 nasm_error(ERR_NONFATAL, "invalid ..got reference");
814 break;
816 } else if (wrt == elf_sym_sect + 1) {
817 switch (isize) {
818 case 1:
819 case -1:
820 elf_add_gsym_reloc(s, segment, addr, 0,
821 R_X86_64_8, false);
822 addr = 0;
823 break;
824 case 2:
825 case -2:
826 elf_add_gsym_reloc(s, segment, addr, 0,
827 R_X86_64_16, false);
828 addr = 0;
829 break;
830 case 4:
831 elf_add_gsym_reloc(s, segment, addr, 0,
832 R_X86_64_32, false);
833 addr = 0;
834 break;
835 case -4:
836 elf_add_gsym_reloc(s, segment, addr, 0,
837 R_X86_64_32S, false);
838 addr = 0;
839 break;
840 case 8:
841 case -8:
842 elf_add_gsym_reloc(s, segment, addr, 0,
843 R_X86_64_64, false);
844 addr = 0;
845 break;
846 default:
847 nasm_error(ERR_PANIC, "internal error elf64-hpa-903");
848 break;
850 } else if (wrt == elf_plt_sect + 1) {
851 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
852 "relative PLT references");
853 } else {
854 nasm_error(ERR_NONFATAL, "ELF format does not support this"
855 " use of WRT");
858 elf_sect_writeaddr(s, addr, asize);
859 break;
862 case OUT_REL1ADR:
863 reltype = R_X86_64_PC8;
864 bytes = 1;
865 goto rel12adr;
867 case OUT_REL2ADR:
868 reltype = R_X86_64_PC16;
869 bytes = 2;
870 goto rel12adr;
872 rel12adr:
873 addr = *(int64_t *)data - size;
874 if (segment == segto)
875 nasm_error(ERR_PANIC, "intra-segment OUT_REL1ADR");
876 if (segment == NO_SEG) {
877 /* Do nothing */
878 } else if (segment % 2) {
879 nasm_error(ERR_NONFATAL, "ELF format does not support"
880 " segment base references");
881 } else {
882 if (wrt == NO_SEG) {
883 elf_add_reloc(s, segment, addr, reltype);
884 addr = 0;
885 } else {
886 nasm_error(ERR_NONFATAL,
887 "Unsupported non-32-bit ELF relocation");
890 elf_sect_writeaddr(s, addr, bytes);
891 break;
893 case OUT_REL4ADR:
894 addr = *(int64_t *)data - size;
895 if (segment == segto)
896 nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
897 if (segment == NO_SEG) {
898 /* Do nothing */
899 } else if (segment % 2) {
900 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
901 " segment base references");
902 } else {
903 if (wrt == NO_SEG) {
904 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
905 addr = 0;
906 } else if (wrt == elf_plt_sect + 1) {
907 elf_add_gsym_reloc(s, segment, addr+size, size,
908 R_X86_64_PLT32, true);
909 addr = 0;
910 } else if (wrt == elf_gotpc_sect + 1 ||
911 wrt == elf_got_sect + 1) {
912 elf_add_gsym_reloc(s, segment, addr+size, size,
913 R_X86_64_GOTPCREL, true);
914 addr = 0;
915 } else if (wrt == elf_gotoff_sect + 1 ||
916 wrt == elf_got_sect + 1) {
917 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
918 "qword absolute");
919 } else if (wrt == elf_gottpoff_sect + 1) {
920 elf_add_gsym_reloc(s, segment, addr+size, size,
921 R_X86_64_GOTTPOFF, true);
922 addr = 0;
923 } else {
924 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
925 " use of WRT");
928 elf_sect_writeaddr(s, addr, 4);
929 break;
931 case OUT_REL8ADR:
932 addr = *(int64_t *)data - size;
933 if (segment == segto)
934 nasm_error(ERR_PANIC, "intra-segment OUT_REL8ADR");
935 if (segment == NO_SEG) {
936 /* Do nothing */
937 } else if (segment % 2) {
938 nasm_error(ERR_NONFATAL, "ELF64 format does not support"
939 " segment base references");
940 } else {
941 if (wrt == NO_SEG) {
942 elf_add_reloc(s, segment, addr, R_X86_64_PC64);
943 addr = 0;
944 } else if (wrt == elf_gotpc_sect + 1 ||
945 wrt == elf_got_sect + 1) {
946 elf_add_gsym_reloc(s, segment, addr+size, size,
947 R_X86_64_GOTPCREL64, true);
948 addr = 0;
949 } else if (wrt == elf_gotoff_sect + 1 ||
950 wrt == elf_got_sect + 1) {
951 nasm_error(ERR_NONFATAL, "ELF64 requires ..gotoff references to be "
952 "absolute");
953 } else if (wrt == elf_gottpoff_sect + 1) {
954 nasm_error(ERR_NONFATAL, "ELF64 requires ..gottpoff references to be "
955 "dword");
956 } else {
957 nasm_error(ERR_NONFATAL, "ELF64 format does not support this"
958 " use of WRT");
961 elf_sect_writeaddr(s, addr, 8);
962 break;
966 static void elf_write(void)
968 int align;
969 char *p;
970 int i;
972 struct SAA *symtab;
973 int32_t symtablen, symtablocal;
976 * Work out how many sections we will have. We have SHN_UNDEF,
977 * then the flexible user sections, then the fixed sections
978 * `.shstrtab', `.symtab' and `.strtab', then optionally
979 * relocation sections for the user sections.
981 nsections = sec_numspecial + 1;
982 if (of_elf64.current_dfmt == &df_stabs)
983 nsections += 3;
984 else if (of_elf64.current_dfmt == &df_dwarf)
985 nsections += 10;
987 add_sectname("", ".shstrtab");
988 add_sectname("", ".symtab");
989 add_sectname("", ".strtab");
990 for (i = 0; i < nsects; i++) {
991 nsections++; /* for the section itself */
992 if (sects[i]->head) {
993 nsections++; /* for its relocations */
994 add_sectname(".rela", sects[i]->name);
998 if (of_elf64.current_dfmt == &df_stabs) {
999 /* in case the debug information is wanted, just add these three sections... */
1000 add_sectname("", ".stab");
1001 add_sectname("", ".stabstr");
1002 add_sectname(".rel", ".stab");
1005 else if (of_elf64.current_dfmt == &df_dwarf) {
1006 /* the dwarf debug standard specifies the following ten sections,
1007 not all of which are currently implemented,
1008 although all of them are defined. */
1009 #define debug_aranges (int64_t) (nsections-10)
1010 #define debug_info (int64_t) (nsections-7)
1011 #define debug_abbrev (int64_t) (nsections-5)
1012 #define debug_line (int64_t) (nsections-4)
1013 add_sectname("", ".debug_aranges");
1014 add_sectname(".rela", ".debug_aranges");
1015 add_sectname("", ".debug_pubnames");
1016 add_sectname("", ".debug_info");
1017 add_sectname(".rela", ".debug_info");
1018 add_sectname("", ".debug_abbrev");
1019 add_sectname("", ".debug_line");
1020 add_sectname(".rela", ".debug_line");
1021 add_sectname("", ".debug_frame");
1022 add_sectname("", ".debug_loc");
1026 * Output the ELF header.
1028 nasm_write("\177ELF\2\1\1", 7, ofile);
1029 fputc(elf_osabi, ofile);
1030 fputc(elf_abiver, ofile);
1031 fwritezero(7, ofile);
1032 fwriteint16_t(ET_REL, ofile); /* relocatable file */
1033 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
1034 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
1035 fwriteint64_t(0L, ofile); /* no entry point */
1036 fwriteint64_t(0L, ofile); /* no program header table */
1037 fwriteint64_t(0x40L, ofile); /* section headers straight after
1038 * ELF header plus alignment */
1039 fwriteint32_t(0L, ofile); /* 386 defines no special flags */
1040 fwriteint16_t(0x40, ofile); /* size of ELF header */
1041 fwriteint16_t(0, ofile); /* no program header table, again */
1042 fwriteint16_t(0, ofile); /* still no program header table */
1043 fwriteint16_t(sizeof(Elf64_Shdr), ofile); /* size of section header */
1044 fwriteint16_t(nsections, ofile); /* number of sections */
1045 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
1046 * section header table */
1049 * Build the symbol table and relocation tables.
1051 symtab = elf_build_symtab(&symtablen, &symtablocal);
1052 for (i = 0; i < nsects; i++)
1053 if (sects[i]->head)
1054 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1055 sects[i]->head);
1058 * Now output the section header table.
1061 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1062 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1063 elf_foffs += align;
1064 elf_nsect = 0;
1065 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1067 /* SHN_UNDEF */
1068 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1069 p = shstrtab + 1;
1071 /* The normal sections */
1072 for (i = 0; i < nsects; i++) {
1073 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1074 (sects[i]->type == SHT_PROGBITS ?
1075 sects[i]->data : NULL), true,
1076 sects[i]->len, 0, 0, sects[i]->align, 0);
1077 p += strlen(p) + 1;
1080 /* .shstrtab */
1081 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1082 shstrtablen, 0, 0, 1, 0);
1083 p += strlen(p) + 1;
1085 /* .symtab */
1086 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1087 symtablen, sec_strtab, symtablocal, 8, 24);
1088 p += strlen(p) + 1;
1090 /* .strtab */
1091 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1092 strslen, 0, 0, 1, 0);
1093 p += strlen(p) + 1;
1095 /* The relocation sections */
1096 for (i = 0; i < nsects; i++)
1097 if (sects[i]->head) {
1098 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1099 sects[i]->rellen, sec_symtab, i + 1, 8, 24);
1100 p += strlen(p) + 1;
1103 if (of_elf64.current_dfmt == &df_stabs) {
1104 /* for debugging information, create the last three sections
1105 which are the .stab , .stabstr and .rel.stab sections respectively */
1107 /* this function call creates the stab sections in memory */
1108 stabs64_generate();
1110 if (stabbuf && stabstrbuf && stabrelbuf) {
1111 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1112 stablen, sec_stabstr, 0, 4, 12);
1113 p += strlen(p) + 1;
1115 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1116 stabstrlen, 0, 0, 4, 0);
1117 p += strlen(p) + 1;
1119 /* link -> symtable info -> section to refer to */
1120 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1121 stabrellen, symtabsection, sec_stab, 4, 16);
1122 p += strlen(p) + 1;
1124 } else if (of_elf64.current_dfmt == &df_dwarf) {
1125 /* for dwarf debugging information, create the ten dwarf sections */
1127 /* this function call creates the dwarf sections in memory */
1128 if (dwarf_fsect)
1129 dwarf64_generate();
1131 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1132 arangeslen, 0, 0, 1, 0);
1133 p += strlen(p) + 1;
1135 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1136 arangesrellen, symtabsection, debug_aranges, 1, 24);
1137 p += strlen(p) + 1;
1139 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1140 pubnameslen, 0, 0, 1, 0);
1141 p += strlen(p) + 1;
1143 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1144 infolen, 0, 0, 1, 0);
1145 p += strlen(p) + 1;
1147 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1148 inforellen, symtabsection, debug_info, 1, 24);
1149 p += strlen(p) + 1;
1151 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1152 abbrevlen, 0, 0, 1, 0);
1153 p += strlen(p) + 1;
1155 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1156 linelen, 0, 0, 1, 0);
1157 p += strlen(p) + 1;
1159 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1160 linerellen, symtabsection, debug_line, 1, 24);
1161 p += strlen(p) + 1;
1163 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1164 framelen, 0, 0, 8, 0);
1165 p += strlen(p) + 1;
1167 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1168 loclen, 0, 0, 1, 0);
1169 p += strlen(p) + 1;
1171 fwritezero(align, ofile);
1174 * Now output the sections.
1176 elf_write_sections();
1178 nasm_free(elf_sects);
1179 saa_free(symtab);
1182 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1184 struct SAA *s = saa_init(1L);
1185 struct elf_symbol *sym;
1186 uint8_t entry[24], *p;
1187 int i;
1189 *len = *local = 0;
1192 * First, an all-zeros entry, required by the ELF spec.
1194 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1195 *len += 24;
1196 (*local)++;
1199 * Next, an entry for the file name.
1201 p = entry;
1202 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1203 WRITESHORT(p, STT_FILE); /* type FILE */
1204 WRITESHORT(p, SHN_ABS);
1205 WRITEDLONG(p, (uint64_t) 0); /* no value */
1206 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1207 saa_wbytes(s, entry, 24L);
1208 *len += 24;
1209 (*local)++;
1212 * Now some standard symbols defining the segments, for relocation
1213 * purposes.
1215 for (i = 1; i <= nsects; i++) {
1216 p = entry;
1217 WRITELONG(p, 0); /* no symbol name */
1218 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1219 WRITESHORT(p, i); /* section id */
1220 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1221 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1222 saa_wbytes(s, entry, 24L);
1223 *len += 24;
1224 (*local)++;
1229 * Now the other local symbols.
1231 saa_rewind(syms);
1232 while ((sym = saa_rstruct(syms))) {
1233 if (sym->type & SYM_GLOBAL)
1234 continue;
1235 p = entry;
1236 WRITELONG(p, sym->strpos); /* index into symbol string table */
1237 WRITECHAR(p, sym->type); /* type and binding */
1238 WRITECHAR(p, sym->other); /* visibility */
1239 WRITESHORT(p, sym->section); /* index into section header table */
1240 WRITEDLONG(p, (int64_t)sym->symv.key); /* value of symbol */
1241 WRITEDLONG(p, (int64_t)sym->size); /* size of symbol */
1242 saa_wbytes(s, entry, 24L);
1243 *len += 24;
1244 (*local)++;
1247 * dwarf needs symbols for debug sections
1248 * which are relocation targets.
1250 if (of_elf64.current_dfmt == &df_dwarf) {
1251 dwarf_infosym = *local;
1252 p = entry;
1253 WRITELONG(p, 0); /* no symbol name */
1254 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1255 WRITESHORT(p, debug_info); /* 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_abbrevsym = *local;
1262 p = entry;
1263 WRITELONG(p, 0); /* no symbol name */
1264 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1265 WRITESHORT(p, debug_abbrev); /* 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)++;
1271 dwarf_linesym = *local;
1272 p = entry;
1273 WRITELONG(p, 0); /* no symbol name */
1274 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1275 WRITESHORT(p, debug_line); /* section id */
1276 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1277 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1278 saa_wbytes(s, entry, 24L);
1279 *len += 24;
1280 (*local)++;
1284 * Now the global symbols.
1286 saa_rewind(syms);
1287 while ((sym = saa_rstruct(syms))) {
1288 if (!(sym->type & SYM_GLOBAL))
1289 continue;
1290 p = entry;
1291 WRITELONG(p, sym->strpos);
1292 WRITECHAR(p, sym->type); /* type and binding */
1293 WRITECHAR(p, sym->other); /* visibility */
1294 WRITESHORT(p, sym->section);
1295 WRITEDLONG(p, (int64_t)sym->symv.key);
1296 WRITEDLONG(p, (int64_t)sym->size);
1297 saa_wbytes(s, entry, 24L);
1298 *len += 24;
1301 return s;
1304 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1306 struct SAA *s;
1307 uint8_t *p, entry[24];
1308 int32_t global_offset;
1310 if (!r)
1311 return NULL;
1313 s = saa_init(1L);
1314 *len = 0;
1317 * How to onvert from a global placeholder to a real symbol index;
1318 * the +2 refers to the two special entries, the null entry and
1319 * the filename entry.
1321 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1323 while (r) {
1324 int32_t sym = r->symbol;
1326 if (sym >= GLOBAL_TEMP_BASE)
1327 sym += global_offset;
1329 p = entry;
1330 WRITEDLONG(p, r->address);
1331 WRITELONG(p, r->type);
1332 WRITELONG(p, sym);
1333 WRITEDLONG(p, r->offset);
1334 saa_wbytes(s, entry, 24L);
1335 *len += 24;
1337 r = r->next;
1340 return s;
1343 static void elf_section_header(int name, int type, uint64_t flags,
1344 void *data, bool is_saa, uint64_t datalen,
1345 int link, int info, int align, int eltsize)
1347 elf_sects[elf_nsect].data = data;
1348 elf_sects[elf_nsect].len = datalen;
1349 elf_sects[elf_nsect].is_saa = is_saa;
1350 elf_nsect++;
1352 fwriteint32_t((int32_t)name, ofile);
1353 fwriteint32_t((int32_t)type, ofile);
1354 fwriteint64_t((int64_t)flags, ofile);
1355 fwriteint64_t(0L, ofile); /* no address, ever, in object files */
1356 fwriteint64_t(type == 0 ? 0L : elf_foffs, ofile);
1357 fwriteint64_t(datalen, ofile);
1358 if (data)
1359 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1360 fwriteint32_t((int32_t)link, ofile);
1361 fwriteint32_t((int32_t)info, ofile);
1362 fwriteint64_t((int64_t)align, ofile);
1363 fwriteint64_t((int64_t)eltsize, ofile);
1366 static void elf_write_sections(void)
1368 int i;
1369 for (i = 0; i < elf_nsect; i++)
1370 if (elf_sects[i].data) {
1371 int32_t len = elf_sects[i].len;
1372 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1373 int32_t align = reallen - len;
1374 if (elf_sects[i].is_saa)
1375 saa_fpwrite(elf_sects[i].data, ofile);
1376 else
1377 nasm_write(elf_sects[i].data, len, ofile);
1378 fwritezero(align, ofile);
1382 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
1384 saa_wbytes(sect->data, data, len);
1385 sect->len += len;
1388 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
1390 saa_writeaddr(sect->data, data, len);
1391 sect->len += len;
1394 static void elf_sectalign(int32_t seg, unsigned int value)
1396 struct elf_section *s = NULL;
1397 int i;
1399 for (i = 0; i < nsects; i++) {
1400 if (sects[i]->index == seg) {
1401 s = sects[i];
1402 break;
1405 if (!s || !is_power2(value))
1406 return;
1408 if (value > s->align)
1409 s->align = value;
1412 static int32_t elf_segbase(int32_t segment)
1414 return segment;
1417 static void elf_filename(char *inname, char *outname)
1419 strcpy(elf_module, inname);
1420 standard_extension(inname, outname, ".o");
1423 extern macros_t elf_stdmac[];
1425 static int elf_set_info(enum geninfo type, char **val)
1427 (void)type;
1428 (void)val;
1429 return 0;
1431 static struct dfmt df_dwarf = {
1432 "ELF64 (x86-64) dwarf debug format for Linux/Unix",
1433 "dwarf",
1434 dwarf64_init,
1435 dwarf64_linenum,
1436 debug64_deflabel,
1437 debug64_directive,
1438 debug64_typevalue,
1439 dwarf64_output,
1440 dwarf64_cleanup
1442 static struct dfmt df_stabs = {
1443 "ELF64 (x86-64) stabs debug format for Linux/Unix",
1444 "stabs",
1445 null_debug_init,
1446 stabs64_linenum,
1447 debug64_deflabel,
1448 debug64_directive,
1449 debug64_typevalue,
1450 stabs64_output,
1451 stabs64_cleanup
1454 struct dfmt *elf64_debugs_arr[3] = { &df_dwarf, &df_stabs, NULL };
1456 struct ofmt of_elf64 = {
1457 "ELF64 (x86_64) object files (e.g. Linux)",
1458 "elf64",
1460 elf64_debugs_arr,
1461 &df_stabs,
1462 elf_stdmac,
1463 elf_init,
1464 elf_set_info,
1465 elf_out,
1466 elf_deflabel,
1467 elf_section_names,
1468 elf_sectalign,
1469 elf_segbase,
1470 elf_directive,
1471 elf_filename,
1472 elf_cleanup
1475 /* common debugging routines */
1476 static void debug64_deflabel(char *name, int32_t segment, int64_t offset,
1477 int is_global, char *special)
1479 (void)name;
1480 (void)segment;
1481 (void)offset;
1482 (void)is_global;
1483 (void)special;
1486 static void debug64_directive(const char *directive, const char *params)
1488 (void)directive;
1489 (void)params;
1492 static void debug64_typevalue(int32_t type)
1494 int32_t stype, ssize;
1495 switch (TYM_TYPE(type)) {
1496 case TY_LABEL:
1497 ssize = 0;
1498 stype = STT_NOTYPE;
1499 break;
1500 case TY_BYTE:
1501 ssize = 1;
1502 stype = STT_OBJECT;
1503 break;
1504 case TY_WORD:
1505 ssize = 2;
1506 stype = STT_OBJECT;
1507 break;
1508 case TY_DWORD:
1509 ssize = 4;
1510 stype = STT_OBJECT;
1511 break;
1512 case TY_FLOAT:
1513 ssize = 4;
1514 stype = STT_OBJECT;
1515 break;
1516 case TY_QWORD:
1517 ssize = 8;
1518 stype = STT_OBJECT;
1519 break;
1520 case TY_TBYTE:
1521 ssize = 10;
1522 stype = STT_OBJECT;
1523 break;
1524 case TY_OWORD:
1525 ssize = 16;
1526 stype = STT_OBJECT;
1527 break;
1528 case TY_YWORD:
1529 ssize = 32;
1530 stype = STT_OBJECT;
1531 break;
1532 case TY_COMMON:
1533 ssize = 0;
1534 stype = STT_COMMON;
1535 break;
1536 case TY_SEG:
1537 ssize = 0;
1538 stype = STT_SECTION;
1539 break;
1540 case TY_EXTERN:
1541 ssize = 0;
1542 stype = STT_NOTYPE;
1543 break;
1544 case TY_EQU:
1545 ssize = 0;
1546 stype = STT_NOTYPE;
1547 break;
1548 default:
1549 ssize = 0;
1550 stype = STT_NOTYPE;
1551 break;
1553 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1554 lastsym->size = ssize;
1555 lastsym->type = stype;
1559 /* stabs debugging routines */
1561 static void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1563 (void)segto;
1564 if (!stabs_filename) {
1565 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1566 strcpy(stabs_filename, filename);
1567 } else {
1568 if (strcmp(stabs_filename, filename)) {
1569 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1570 in fact, this leak comes in quite handy to maintain a list of files
1571 encountered so far in the symbol lines... */
1573 /* why not nasm_free(stabs_filename); we're done with the old one */
1575 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1576 strcpy(stabs_filename, filename);
1579 debug_immcall = 1;
1580 currentline = linenumber;
1584 static void stabs64_output(int type, void *param)
1586 struct symlininfo *s;
1587 struct linelist *el;
1588 if (type == TY_DEBUGSYMLIN) {
1589 if (debug_immcall) {
1590 s = (struct symlininfo *)param;
1591 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1592 return; /* line info is only collected for executable sections */
1593 numlinestabs++;
1594 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1595 el->info.offset = s->offset;
1596 el->info.section = s->section;
1597 el->info.name = s->name;
1598 el->line = currentline;
1599 el->filename = stabs_filename;
1600 el->next = 0;
1601 if (stabslines) {
1602 stabslines->last->next = el;
1603 stabslines->last = el;
1604 } else {
1605 stabslines = el;
1606 stabslines->last = el;
1610 debug_immcall = 0;
1613 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1615 static void stabs64_generate(void)
1617 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1618 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1619 char **allfiles;
1620 int *fileidx;
1622 struct linelist *ptr;
1624 ptr = stabslines;
1626 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1627 numfiles = 0;
1628 while (ptr) {
1629 if (numfiles == 0) {
1630 allfiles[0] = ptr->filename;
1631 numfiles++;
1632 } else {
1633 for (i = 0; i < numfiles; i++) {
1634 if (!strcmp(allfiles[i], ptr->filename))
1635 break;
1637 if (i >= numfiles) {
1638 allfiles[i] = ptr->filename;
1639 numfiles++;
1642 ptr = ptr->next;
1644 strsize = 1;
1645 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1646 for (i = 0; i < numfiles; i++) {
1647 fileidx[i] = strsize;
1648 strsize += strlen(allfiles[i]) + 1;
1650 mainfileindex = 0;
1651 for (i = 0; i < numfiles; i++) {
1652 if (!strcmp(allfiles[i], elf_module)) {
1653 mainfileindex = i;
1654 break;
1659 * worst case size of the stab buffer would be:
1660 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1661 * plus one "ending" entry
1663 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1664 sizeof(struct stabentry));
1665 ssbuf = (uint8_t *)nasm_malloc(strsize);
1666 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1667 rptr = rbuf;
1669 for (i = 0; i < numfiles; i++)
1670 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1671 ssbuf[0] = 0;
1673 stabstrlen = strsize; /* set global variable for length of stab strings */
1675 sptr = sbuf;
1676 ptr = stabslines;
1677 numstabs = 0;
1679 if (ptr) {
1681 * this is the first stab, its strx points to the filename of the
1682 * the source-file, the n_desc field should be set to the number
1683 * of remaining stabs
1685 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1687 /* this is the stab for the main source file */
1688 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1690 /* relocation table entry */
1693 * Since the symbol table has two entries before
1694 * the section symbols, the index in the info.section
1695 * member must be adjusted by adding 2
1698 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1699 WRITELONG(rptr, R_X86_64_32);
1700 WRITELONG(rptr, ptr->info.section + 2);
1702 numstabs++;
1703 currfile = mainfileindex;
1706 while (ptr) {
1707 if (strcmp(allfiles[currfile], ptr->filename)) {
1708 /* oops file has changed... */
1709 for (i = 0; i < numfiles; i++)
1710 if (!strcmp(allfiles[i], ptr->filename))
1711 break;
1712 currfile = i;
1713 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1714 ptr->info.offset);
1715 numstabs++;
1717 /* relocation table entry */
1719 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1720 WRITELONG(rptr, R_X86_64_32);
1721 WRITELONG(rptr, ptr->info.section + 2);
1724 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1725 numstabs++;
1727 /* relocation table entry */
1729 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1730 WRITELONG(rptr, R_X86_64_32);
1731 WRITELONG(rptr, ptr->info.section + 2);
1733 ptr = ptr->next;
1737 /* this is an "ending" token */
1738 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1739 numstabs++;
1741 ((struct stabentry *)sbuf)->n_desc = numstabs;
1743 nasm_free(allfiles);
1744 nasm_free(fileidx);
1746 stablen = (sptr - sbuf);
1747 stabrellen = (rptr - rbuf);
1748 stabrelbuf = rbuf;
1749 stabbuf = sbuf;
1750 stabstrbuf = ssbuf;
1753 static void stabs64_cleanup(void)
1755 struct linelist *ptr, *del;
1756 if (!stabslines)
1757 return;
1759 ptr = stabslines;
1760 while (ptr) {
1761 del = ptr;
1762 ptr = ptr->next;
1763 nasm_free(del);
1766 nasm_free(stabbuf);
1767 nasm_free(stabrelbuf);
1768 nasm_free(stabstrbuf);
1771 /* dwarf routines */
1773 static void dwarf64_init(void)
1775 ndebugs = 3; /* 3 debug symbols */
1778 static void dwarf64_linenum(const char *filename, int32_t linenumber,
1779 int32_t segto)
1781 (void)segto;
1782 dwarf64_findfile(filename);
1783 debug_immcall = 1;
1784 currentline = linenumber;
1787 /* called from elf_out with type == TY_DEBUGSYMLIN */
1788 static void dwarf64_output(int type, void *param)
1790 int ln, aa, inx, maxln, soc;
1791 struct symlininfo *s;
1792 struct SAA *plinep;
1794 (void)type;
1796 s = (struct symlininfo *)param;
1798 /* line number info is only gathered for executable sections */
1799 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1800 return;
1802 /* Check if section index has changed */
1803 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1804 dwarf64_findsect(s->section);
1806 /* do nothing unless line or file has changed */
1807 if (!debug_immcall)
1808 return;
1810 ln = currentline - dwarf_csect->line;
1811 aa = s->offset - dwarf_csect->offset;
1812 inx = dwarf_clist->line;
1813 plinep = dwarf_csect->psaa;
1814 /* check for file change */
1815 if (!(inx == dwarf_csect->file)) {
1816 saa_write8(plinep,DW_LNS_set_file);
1817 saa_write8(plinep,inx);
1818 dwarf_csect->file = inx;
1820 /* check for line change */
1821 if (ln) {
1822 /* test if in range of special op code */
1823 maxln = line_base + line_range;
1824 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1825 if (ln >= line_base && ln < maxln && soc < 256) {
1826 saa_write8(plinep,soc);
1827 } else {
1828 saa_write8(plinep,DW_LNS_advance_line);
1829 saa_wleb128s(plinep,ln);
1830 if (aa) {
1831 saa_write8(plinep,DW_LNS_advance_pc);
1832 saa_wleb128u(plinep,aa);
1835 dwarf_csect->line = currentline;
1836 dwarf_csect->offset = s->offset;
1839 /* show change handled */
1840 debug_immcall = 0;
1844 static void dwarf64_generate(void)
1846 uint8_t *pbuf;
1847 int indx;
1848 struct linelist *ftentry;
1849 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1850 struct SAA *parangesrel, *plinesrel, *pinforel;
1851 struct sectlist *psect;
1852 size_t saalen, linepoff, totlen, highaddr;
1854 /* write epilogues for each line program range */
1855 /* and build aranges section */
1856 paranges = saa_init(1L);
1857 parangesrel = saa_init(1L);
1858 saa_write16(paranges,3); /* dwarf version */
1859 saa_write64(parangesrel, paranges->datalen+4);
1860 saa_write64(parangesrel, (dwarf_infosym << 32) + R_X86_64_32); /* reloc to info */
1861 saa_write64(parangesrel, 0);
1862 saa_write32(paranges,0); /* offset into info */
1863 saa_write8(paranges,8); /* pointer size */
1864 saa_write8(paranges,0); /* not segmented */
1865 saa_write32(paranges,0); /* padding */
1866 /* iterate though sectlist entries */
1867 psect = dwarf_fsect;
1868 totlen = 0;
1869 highaddr = 0;
1870 for (indx = 0; indx < dwarf_nsections; indx++)
1872 plinep = psect->psaa;
1873 /* Line Number Program Epilogue */
1874 saa_write8(plinep,2); /* std op 2 */
1875 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1876 saa_write8(plinep,DW_LNS_extended_op);
1877 saa_write8(plinep,1); /* operand length */
1878 saa_write8(plinep,DW_LNE_end_sequence);
1879 totlen += plinep->datalen;
1880 /* range table relocation entry */
1881 saa_write64(parangesrel, paranges->datalen + 4);
1882 saa_write64(parangesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
1883 saa_write64(parangesrel, (uint64_t) 0);
1884 /* range table entry */
1885 saa_write64(paranges,0x0000); /* range start */
1886 saa_write64(paranges,sects[psect->section]->len); /* range length */
1887 highaddr += sects[psect->section]->len;
1888 /* done with this entry */
1889 psect = psect->next;
1891 saa_write64(paranges,0); /* null address */
1892 saa_write64(paranges,0); /* null length */
1893 saalen = paranges->datalen;
1894 arangeslen = saalen + 4;
1895 arangesbuf = pbuf = nasm_malloc(arangeslen);
1896 WRITELONG(pbuf,saalen); /* initial length */
1897 saa_rnbytes(paranges, pbuf, saalen);
1898 saa_free(paranges);
1900 /* build rela.aranges section */
1901 arangesrellen = saalen = parangesrel->datalen;
1902 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1903 saa_rnbytes(parangesrel, pbuf, saalen);
1904 saa_free(parangesrel);
1906 /* build pubnames section */
1907 ppubnames = saa_init(1L);
1908 saa_write16(ppubnames,3); /* dwarf version */
1909 saa_write32(ppubnames,0); /* offset into info */
1910 saa_write32(ppubnames,0); /* space used in info */
1911 saa_write32(ppubnames,0); /* end of list */
1912 saalen = ppubnames->datalen;
1913 pubnameslen = saalen + 4;
1914 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1915 WRITELONG(pbuf,saalen); /* initial length */
1916 saa_rnbytes(ppubnames, pbuf, saalen);
1917 saa_free(ppubnames);
1919 /* build info section */
1920 pinfo = saa_init(1L);
1921 pinforel = saa_init(1L);
1922 saa_write16(pinfo,3); /* dwarf version */
1923 saa_write64(pinforel, pinfo->datalen + 4);
1924 saa_write64(pinforel, (dwarf_abbrevsym << 32) + R_X86_64_32); /* reloc to abbrev */
1925 saa_write64(pinforel, 0);
1926 saa_write32(pinfo,0); /* offset into abbrev */
1927 saa_write8(pinfo,8); /* pointer size */
1928 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1929 saa_write64(pinforel, pinfo->datalen + 4);
1930 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1931 saa_write64(pinforel, 0);
1932 saa_write64(pinfo,0); /* DW_AT_low_pc */
1933 saa_write64(pinforel, pinfo->datalen + 4);
1934 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1935 saa_write64(pinforel, 0);
1936 saa_write64(pinfo,highaddr); /* DW_AT_high_pc */
1937 saa_write64(pinforel, pinfo->datalen + 4);
1938 saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
1939 saa_write64(pinforel, 0);
1940 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1941 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1942 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1943 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1944 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1945 saa_write64(pinforel, pinfo->datalen + 4);
1946 saa_write64(pinforel, ((uint64_t)(dwarf_fsect->section + 2) << 32) + R_X86_64_64);
1947 saa_write64(pinforel, 0);
1948 saa_write64(pinfo,0); /* DW_AT_low_pc */
1949 saa_write64(pinfo,0); /* DW_AT_frame_base */
1950 saa_write8(pinfo,0); /* end of entries */
1951 saalen = pinfo->datalen;
1952 infolen = saalen + 4;
1953 infobuf = pbuf = nasm_malloc(infolen);
1954 WRITELONG(pbuf,saalen); /* initial length */
1955 saa_rnbytes(pinfo, pbuf, saalen);
1956 saa_free(pinfo);
1958 /* build rela.info section */
1959 inforellen = saalen = pinforel->datalen;
1960 inforelbuf = pbuf = nasm_malloc(inforellen);
1961 saa_rnbytes(pinforel, pbuf, saalen);
1962 saa_free(pinforel);
1964 /* build abbrev section */
1965 pabbrev = saa_init(1L);
1966 saa_write8(pabbrev,1); /* entry number LEB128u */
1967 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1968 saa_write8(pabbrev,1); /* has children */
1969 /* the following attributes and forms are all LEB128u values */
1970 saa_write8(pabbrev,DW_AT_low_pc);
1971 saa_write8(pabbrev,DW_FORM_addr);
1972 saa_write8(pabbrev,DW_AT_high_pc);
1973 saa_write8(pabbrev,DW_FORM_addr);
1974 saa_write8(pabbrev,DW_AT_stmt_list);
1975 saa_write8(pabbrev,DW_FORM_data4);
1976 saa_write8(pabbrev,DW_AT_name);
1977 saa_write8(pabbrev,DW_FORM_string);
1978 saa_write8(pabbrev,DW_AT_producer);
1979 saa_write8(pabbrev,DW_FORM_string);
1980 saa_write8(pabbrev,DW_AT_language);
1981 saa_write8(pabbrev,DW_FORM_data2);
1982 saa_write16(pabbrev,0); /* end of entry */
1983 /* LEB128u usage same as above */
1984 saa_write8(pabbrev,2); /* entry number */
1985 saa_write8(pabbrev,DW_TAG_subprogram);
1986 saa_write8(pabbrev,0); /* no children */
1987 saa_write8(pabbrev,DW_AT_low_pc);
1988 saa_write8(pabbrev,DW_FORM_addr);
1989 saa_write8(pabbrev,DW_AT_frame_base);
1990 saa_write8(pabbrev,DW_FORM_data4);
1991 saa_write16(pabbrev,0); /* end of entry */
1992 abbrevlen = saalen = pabbrev->datalen;
1993 abbrevbuf = pbuf = nasm_malloc(saalen);
1994 saa_rnbytes(pabbrev, pbuf, saalen);
1995 saa_free(pabbrev);
1997 /* build line section */
1998 /* prolog */
1999 plines = saa_init(1L);
2000 saa_write8(plines,1); /* Minimum Instruction Length */
2001 saa_write8(plines,1); /* Initial value of 'is_stmt' */
2002 saa_write8(plines,line_base); /* Line Base */
2003 saa_write8(plines,line_range); /* Line Range */
2004 saa_write8(plines,opcode_base); /* Opcode Base */
2005 /* standard opcode lengths (# of LEB128u operands) */
2006 saa_write8(plines,0); /* Std opcode 1 length */
2007 saa_write8(plines,1); /* Std opcode 2 length */
2008 saa_write8(plines,1); /* Std opcode 3 length */
2009 saa_write8(plines,1); /* Std opcode 4 length */
2010 saa_write8(plines,1); /* Std opcode 5 length */
2011 saa_write8(plines,0); /* Std opcode 6 length */
2012 saa_write8(plines,0); /* Std opcode 7 length */
2013 saa_write8(plines,0); /* Std opcode 8 length */
2014 saa_write8(plines,1); /* Std opcode 9 length */
2015 saa_write8(plines,0); /* Std opcode 10 length */
2016 saa_write8(plines,0); /* Std opcode 11 length */
2017 saa_write8(plines,1); /* Std opcode 12 length */
2018 /* Directory Table */
2019 saa_write8(plines,0); /* End of table */
2020 /* File Name Table */
2021 ftentry = dwarf_flist;
2022 for (indx = 0;indx<dwarf_numfiles;indx++)
2024 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2025 saa_write8(plines,0); /* directory LEB128u */
2026 saa_write8(plines,0); /* time LEB128u */
2027 saa_write8(plines,0); /* size LEB128u */
2028 ftentry = ftentry->next;
2030 saa_write8(plines,0); /* End of table */
2031 linepoff = plines->datalen;
2032 linelen = linepoff + totlen + 10;
2033 linebuf = pbuf = nasm_malloc(linelen);
2034 WRITELONG(pbuf,linelen-4); /* initial length */
2035 WRITESHORT(pbuf,3); /* dwarf version */
2036 WRITELONG(pbuf,linepoff); /* offset to line number program */
2037 /* write line header */
2038 saalen = linepoff;
2039 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2040 pbuf += linepoff;
2041 saa_free(plines);
2042 /* concatonate line program ranges */
2043 linepoff += 13;
2044 plinesrel = saa_init(1L);
2045 psect = dwarf_fsect;
2046 for (indx = 0; indx < dwarf_nsections; indx++) {
2047 saa_write64(plinesrel, linepoff);
2048 saa_write64(plinesrel, ((uint64_t) (psect->section + 2) << 32) + R_X86_64_64);
2049 saa_write64(plinesrel, (uint64_t) 0);
2050 plinep = psect->psaa;
2051 saalen = plinep->datalen;
2052 saa_rnbytes(plinep, pbuf, saalen);
2053 pbuf += saalen;
2054 linepoff += saalen;
2055 saa_free(plinep);
2056 /* done with this entry */
2057 psect = psect->next;
2061 /* build rela.lines section */
2062 linerellen =saalen = plinesrel->datalen;
2063 linerelbuf = pbuf = nasm_malloc(linerellen);
2064 saa_rnbytes(plinesrel, pbuf, saalen);
2065 saa_free(plinesrel);
2067 /* build frame section */
2068 framelen = 4;
2069 framebuf = pbuf = nasm_malloc(framelen);
2070 WRITELONG(pbuf,framelen-4); /* initial length */
2072 /* build loc section */
2073 loclen = 16;
2074 locbuf = pbuf = nasm_malloc(loclen);
2075 WRITEDLONG(pbuf,0); /* null beginning offset */
2076 WRITEDLONG(pbuf,0); /* null ending offset */
2079 static void dwarf64_cleanup(void)
2081 nasm_free(arangesbuf);
2082 nasm_free(arangesrelbuf);
2083 nasm_free(pubnamesbuf);
2084 nasm_free(infobuf);
2085 nasm_free(inforelbuf);
2086 nasm_free(abbrevbuf);
2087 nasm_free(linebuf);
2088 nasm_free(linerelbuf);
2089 nasm_free(framebuf);
2090 nasm_free(locbuf);
2093 static void dwarf64_findfile(const char * fname)
2095 int finx;
2096 struct linelist *match;
2098 /* return if fname is current file name */
2099 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2100 return;
2102 /* search for match */
2103 match = 0;
2104 if (dwarf_flist) {
2105 match = dwarf_flist;
2106 for (finx = 0; finx < dwarf_numfiles; finx++) {
2107 if (!(strcmp(fname, match->filename))) {
2108 dwarf_clist = match;
2109 return;
2114 /* add file name to end of list */
2115 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2116 dwarf_numfiles++;
2117 dwarf_clist->line = dwarf_numfiles;
2118 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2119 strcpy(dwarf_clist->filename,fname);
2120 dwarf_clist->next = 0;
2121 if (!dwarf_flist) { /* if first entry */
2122 dwarf_flist = dwarf_elist = dwarf_clist;
2123 dwarf_clist->last = 0;
2124 } else { /* chain to previous entry */
2125 dwarf_elist->next = dwarf_clist;
2126 dwarf_elist = dwarf_clist;
2130 static void dwarf64_findsect(const int index)
2132 int sinx;
2133 struct sectlist *match;
2134 struct SAA *plinep;
2136 /* return if index is current section index */
2137 if (dwarf_csect && (dwarf_csect->section == index))
2138 return;
2140 /* search for match */
2141 match = 0;
2142 if (dwarf_fsect) {
2143 match = dwarf_fsect;
2144 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2145 if (match->section == index) {
2146 dwarf_csect = match;
2147 return;
2149 match = match->next;
2153 /* add entry to end of list */
2154 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2155 dwarf_nsections++;
2156 dwarf_csect->psaa = plinep = saa_init(1L);
2157 dwarf_csect->line = 1;
2158 dwarf_csect->offset = 0;
2159 dwarf_csect->file = 1;
2160 dwarf_csect->section = index;
2161 dwarf_csect->next = 0;
2162 /* set relocatable address at start of line program */
2163 saa_write8(plinep,DW_LNS_extended_op);
2164 saa_write8(plinep,9); /* operand length */
2165 saa_write8(plinep,DW_LNE_set_address);
2166 saa_write64(plinep,0); /* Start Address */
2168 if (!dwarf_fsect) { /* if first entry */
2169 dwarf_fsect = dwarf_esect = dwarf_csect;
2170 dwarf_csect->last = 0;
2171 } else { /* chain to previous entry */
2172 dwarf_esect->next = dwarf_csect;
2173 dwarf_esect = dwarf_csect;
2177 #endif /* OF_ELF */