1 /* outelf64.c output routines for the Netwide Assembler to produce
2 * ELF64 (x86_64 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the license given in the file "LICENSE"
7 * distributed in the NASM archive.
23 /* Definitions in lieu of elf.h */
24 #define SHT_NULL 0 /* Inactive section header */
25 #define SHT_PROGBITS 1 /* Program defined content */
26 #define SHT_RELA 4 /* Relocation entries with addends */
27 #define SHT_NOBITS 8 /* Section requires no space in file */
28 #define SHF_WRITE (1 << 0) /* Writable */
29 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
30 #define SHF_EXECINSTR (1 << 2) /* Executable */
31 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
32 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
33 #define R_X86_64_NONE 0 /* No reloc */
34 #define R_X86_64_64 1 /* Direct 64 bit address */
35 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
36 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
37 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
38 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
39 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
40 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
41 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
42 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
43 #define ET_REL 1 /* Relocatable file */
44 #define EM_X86_64 62 /* AMD x86-64 architecture */
45 #define STT_NOTYPE 0 /* Symbol type is unspecified */
46 #define STT_OBJECT 1 /* Symbol is a data object */
47 #define STT_FUNC 2 /* Symbol is a code object */
48 #define STT_SECTION 3 /* Symbol associated with a section */
49 #define STT_FILE 4 /* Symbol's name is file name */
50 #define STT_COMMON 5 /* Symbol is a common data object */
51 #define STT_TLS 6 /* Symbol is thread-local data object*/
52 #define STT_NUM 7 /* Number of defined types. */
54 /* Definitions in lieu of dwarf.h */
55 #define DW_TAG_compile_unit 0x11
56 #define DW_TAG_subprogram 0x2e
57 #define DW_AT_name 0x03
58 #define DW_AT_stmt_list 0x10
59 #define DW_AT_low_pc 0x11
60 #define DW_AT_high_pc 0x12
61 #define DW_AT_language 0x13
62 #define DW_AT_producer 0x25
63 #define DW_AT_frame_base 0x40
64 #define DW_FORM_addr 0x01
65 #define DW_FORM_data2 0x05
66 #define DW_FORM_data4 0x06
67 #define DW_FORM_string 0x08
68 #define DW_LNS_extended_op 0
69 #define DW_LNS_advance_pc 2
70 #define DW_LNS_advance_line 3
71 #define DW_LNS_set_file 4
72 #define DW_LNE_end_sequence 1
73 #define DW_LNE_set_address 2
74 #define DW_LNE_define_file 3
75 #define DW_LANG_Mips_Assembler 0x8001
77 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
79 typedef uint32_t Elf64_Word
;
80 typedef uint64_t Elf64_Xword
;
81 typedef uint64_t Elf64_Addr
;
82 typedef uint64_t Elf64_Off
;
85 Elf64_Word sh_name
; /* Section name (string tbl index) */
86 Elf64_Word sh_type
; /* Section type */
87 Elf64_Xword sh_flags
; /* Section flags */
88 Elf64_Addr sh_addr
; /* Section virtual addr at execution */
89 Elf64_Off sh_offset
; /* Section file offset */
90 Elf64_Xword sh_size
; /* Section size in bytes */
91 Elf64_Word sh_link
; /* Link to another section */
92 Elf64_Word sh_info
; /* Additional section information */
93 Elf64_Xword sh_addralign
; /* Section alignment */
94 Elf64_Xword sh_entsize
; /* Entry size if section holds table */
103 int64_t address
; /* relative to _start_ of section */
104 int64_t symbol
; /* symbol index */
105 int type
; /* type of relocation */
109 int32_t strpos
; /* string table position of name */
110 int32_t section
; /* section ID of the symbol */
111 int type
; /* symbol type */
112 int other
; /* symbol visibility */
113 int64_t value
; /* address, or COMMON variable align */
114 int32_t size
; /* size of symbol */
115 int32_t globnum
; /* symbol table offset if global */
116 struct Symbol
*next
; /* list of globals in each section */
117 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
118 char *name
; /* used temporarily if in above list */
126 int32_t index
; /* index into sects array */
127 uint32_t type
; /* SHT_PROGBITS or SHT_NOBITS */
128 uint64_t align
; /* alignment: power of two */
129 uint64_t flags
; /* section flags */
133 struct Reloc
*head
, **tail
;
134 struct Symbol
*gsyms
; /* global symbols in section */
137 #define SECT_DELTA 32
138 static struct Section
**sects
;
139 static int nsects
, sectlen
;
141 #define SHSTR_DELTA 256
142 static char *shstrtab
;
143 static int shstrtablen
, shstrtabsize
;
145 static struct SAA
*syms
;
146 static uint32_t nlocals
, nglobs
;
148 static int32_t def_seg
;
150 static struct RAA
*bsym
;
152 static struct SAA
*strs
;
153 static uint32_t strslen
;
157 static evalfunc evaluate
;
159 static struct Symbol
*fwds
;
161 static char elf_module
[FILENAME_MAX
];
163 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
164 static uint8_t elf_abiver
= 0; /* Current ABI version */
166 extern struct ofmt of_elf64
;
170 #define SYM_GLOBAL 0x10
172 #define STV_DEFAULT 0
173 #define STV_INTERNAL 1
175 #define STV_PROTECTED 3
177 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
179 #define SEG_ALIGN 16 /* alignment of sections in file */
180 #define SEG_ALIGN_1 (SEG_ALIGN-1)
182 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
184 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
186 static struct ELF_SECTDATA
{
191 static int elf_nsect
, nsections
;
192 static int64_t elf_foffs
;
194 static void elf_write(void);
195 static void elf_sect_write(struct Section
*, const uint8_t *,
197 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
199 static void elf_write_sections(void);
200 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
201 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
202 static void add_sectname(char *, char *);
204 /* type values for stabs debugging sections */
205 #define N_SO 0x64 /* ID for main source file */
206 #define N_SOL 0x84 /* ID for sub-source file */
207 #define N_BINCL 0x82 /* not currently used */
208 #define N_EINCL 0xA2 /* not currently used */
225 int section
; /* index into sects[] */
226 int segto
; /* internal section number */
227 char *name
; /* shallow-copied pointer of section name */
231 struct symlininfo info
;
234 struct linelist
*next
;
235 struct linelist
*last
;
244 struct sectlist
*next
;
245 struct sectlist
*last
;
248 /* common debug variables */
249 static int currentline
= 1;
250 static int debug_immcall
= 0;
252 /* stabs debug variables */
253 static struct linelist
*stabslines
= 0;
254 static int numlinestabs
= 0;
255 static char *stabs_filename
= 0;
256 static int symtabsection
;
257 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
258 static int stablen
, stabstrlen
, stabrellen
;
260 /* dwarf debug variables */
261 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
262 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
263 static int dwarf_numfiles
= 0, dwarf_nsections
;
264 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
265 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
266 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
267 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
268 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
269 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
270 static char workbuf
[1024];
273 static struct dfmt df_dwarf
;
274 static struct dfmt df_stabs
;
275 static struct Symbol
*lastsym
;
277 /* common debugging routines */
278 void debug64_typevalue(int32_t);
279 void debug64_init(struct ofmt
*, void *, FILE *, efunc
);
280 void debug64_deflabel(char *, int32_t, int64_t, int, char *);
281 void debug64_directive(const char *, const char *);
283 /* stabs debugging routines */
284 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
285 void stabs64_output(int, void *);
286 void stabs64_generate(void);
287 void stabs64_cleanup(void);
289 /* dwarf debugging routines */
290 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
291 void dwarf64_output(int, void *);
292 void dwarf64_generate(void);
293 void dwarf64_cleanup(void);
294 void dwarf64_findfile(const char *);
295 void dwarf64_findsect(const int);
298 * Special section numbers which are used to define ELF special
299 * symbols, which can be used with WRT to provide PIC relocation
302 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
303 static int32_t elf_got_sect
, elf_plt_sect
;
304 static int32_t elf_sym_sect
;
306 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
312 (void)ldef
; /* placate optimisers */
314 nsects
= sectlen
= 0;
315 syms
= saa_init((int32_t)sizeof(struct Symbol
));
316 nlocals
= nglobs
= 0;
319 saa_wbytes(strs
, "\0", 1L);
320 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
321 strslen
= 2 + strlen(elf_module
);
323 shstrtablen
= shstrtabsize
= 0;;
324 add_sectname("", "");
328 elf_gotpc_sect
= seg_alloc();
329 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
331 elf_gotoff_sect
= seg_alloc();
332 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
334 elf_got_sect
= seg_alloc();
335 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
337 elf_plt_sect
= seg_alloc();
338 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
340 elf_sym_sect
= seg_alloc();
341 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
344 def_seg
= seg_alloc();
348 static void elf_cleanup(int debuginfo
)
357 for (i
= 0; i
< nsects
; i
++) {
358 if (sects
[i
]->type
!= SHT_NOBITS
)
359 saa_free(sects
[i
]->data
);
361 saa_free(sects
[i
]->rel
);
362 while (sects
[i
]->head
) {
364 sects
[i
]->head
= sects
[i
]->head
->next
;
372 if (of_elf64
.current_dfmt
) {
373 of_elf64
.current_dfmt
->cleanup();
376 /* add entry to the elf .shstrtab section */
377 static void add_sectname(char *firsthalf
, char *secondhalf
)
379 int len
= strlen(firsthalf
) + strlen(secondhalf
);
380 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
381 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
382 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
383 strcat(shstrtab
+ shstrtablen
, secondhalf
);
384 shstrtablen
+= len
+ 1;
387 static int elf_make_section(char *name
, int type
, int flags
, int align
)
391 s
= nasm_malloc(sizeof(*s
));
393 if (type
!= SHT_NOBITS
)
394 s
->data
= saa_init(1L);
397 s
->len
= s
->size
= 0;
399 if (!strcmp(name
, ".text"))
402 s
->index
= seg_alloc();
403 add_sectname("", name
);
404 s
->name
= nasm_malloc(1 + strlen(name
));
405 strcpy(s
->name
, name
);
411 if (nsects
>= sectlen
)
413 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
419 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
422 unsigned flags_and
, flags_or
;
423 uint64_t type
, align
;
427 * Default is 64 bits.
435 while (*p
&& !isspace(*p
))
439 flags_and
= flags_or
= type
= align
= 0;
441 while (*p
&& isspace(*p
))
445 while (*p
&& !isspace(*p
))
449 while (*p
&& isspace(*p
))
452 if (!nasm_strnicmp(q
, "align=", 6)) {
456 if ((align
- 1) & align
) { /* means it's not a power of two */
457 error(ERR_NONFATAL
, "section alignment %d is not"
458 " a power of two", align
);
461 } else if (!nasm_stricmp(q
, "alloc")) {
462 flags_and
|= SHF_ALLOC
;
463 flags_or
|= SHF_ALLOC
;
464 } else if (!nasm_stricmp(q
, "noalloc")) {
465 flags_and
|= SHF_ALLOC
;
466 flags_or
&= ~SHF_ALLOC
;
467 } else if (!nasm_stricmp(q
, "exec")) {
468 flags_and
|= SHF_EXECINSTR
;
469 flags_or
|= SHF_EXECINSTR
;
470 } else if (!nasm_stricmp(q
, "noexec")) {
471 flags_and
|= SHF_EXECINSTR
;
472 flags_or
&= ~SHF_EXECINSTR
;
473 } else if (!nasm_stricmp(q
, "write")) {
474 flags_and
|= SHF_WRITE
;
475 flags_or
|= SHF_WRITE
;
476 } else if (!nasm_stricmp(q
, "nowrite")) {
477 flags_and
|= SHF_WRITE
;
478 flags_or
&= ~SHF_WRITE
;
479 } else if (!nasm_stricmp(q
, "progbits")) {
481 } else if (!nasm_stricmp(q
, "nobits")) {
486 if (!strcmp(name
, ".comment") ||
487 !strcmp(name
, ".shstrtab") ||
488 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
489 error(ERR_NONFATAL
, "attempt to redefine reserved section"
494 for (i
= 0; i
< nsects
; i
++)
495 if (!strcmp(name
, sects
[i
]->name
))
498 if (!strcmp(name
, ".text"))
499 i
= elf_make_section(name
, SHT_PROGBITS
,
500 SHF_ALLOC
| SHF_EXECINSTR
, 16);
501 else if (!strcmp(name
, ".rodata"))
502 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
503 else if (!strcmp(name
, ".data"))
504 i
= elf_make_section(name
, SHT_PROGBITS
,
505 SHF_ALLOC
| SHF_WRITE
, 4);
506 else if (!strcmp(name
, ".bss"))
507 i
= elf_make_section(name
, SHT_NOBITS
,
508 SHF_ALLOC
| SHF_WRITE
, 4);
510 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
512 sects
[i
]->type
= type
;
514 sects
[i
]->align
= align
;
515 sects
[i
]->flags
&= ~flags_and
;
516 sects
[i
]->flags
|= flags_or
;
517 } else if (pass
== 1) {
518 if ((type
&& sects
[i
]->type
!= type
)
519 || (align
&& sects
[i
]->align
!= align
)
520 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
521 error(ERR_WARNING
, "incompatible section attributes ignored on"
522 " redeclaration of section `%s'", name
);
525 return sects
[i
]->index
;
528 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
529 int is_global
, char *special
)
533 bool special_used
= false;
535 #if defined(DEBUG) && DEBUG>2
537 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
538 name
, segment
, offset
, is_global
, special
);
540 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
542 * This is a NASM special symbol. We never allow it into
543 * the ELF symbol table, even if it's a valid one. If it
544 * _isn't_ a valid one, we should barf immediately.
546 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
547 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
548 strcmp(name
, "..sym"))
549 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
553 if (is_global
== 3) {
556 * Fix up a forward-reference symbol size from the first
559 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
560 if (!strcmp((*s
)->name
, name
)) {
561 struct tokenval tokval
;
565 while (*p
&& !isspace(*p
))
567 while (*p
&& isspace(*p
))
571 tokval
.t_type
= TOKEN_INVALID
;
572 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
575 error(ERR_NONFATAL
, "cannot use relocatable"
576 " expression as symbol size");
578 (*s
)->size
= reloc_value(e
);
582 * Remove it from the list of unresolved sizes.
584 nasm_free((*s
)->name
);
588 return; /* it wasn't an important one */
591 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
592 strslen
+= 1 + strlen(name
);
594 lastsym
= sym
= saa_wstruct(syms
);
597 sym
->type
= is_global
? SYM_GLOBAL
: 0;
598 sym
->other
= STV_DEFAULT
;
600 if (segment
== NO_SEG
)
601 sym
->section
= SHN_ABS
;
604 sym
->section
= SHN_UNDEF
;
605 if (nsects
== 0 && segment
== def_seg
) {
607 if (segment
!= elf_section_names(".text", 2, &tempint
))
609 "strange segment conditions in ELF driver");
610 sym
->section
= nsects
;
612 for (i
= 0; i
< nsects
; i
++)
613 if (segment
== sects
[i
]->index
) {
614 sym
->section
= i
+ 1;
620 if (is_global
== 2) {
623 sym
->section
= SHN_COMMON
;
625 * We have a common variable. Check the special text to see
626 * if it's a valid number and power of two; if so, store it
627 * as the alignment for the common variable.
631 sym
->value
= readnum(special
, &err
);
633 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
634 " valid number", special
);
635 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
636 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
637 " power of two", special
);
641 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
643 if (sym
->type
== SYM_GLOBAL
) {
645 * If sym->section == SHN_ABS, then the first line of the
646 * else section would cause a core dump, because its a reference
647 * beyond the end of the section array.
648 * This behaviour is exhibited by this code:
651 * To avoid such a crash, such requests are silently discarded.
652 * This may not be the best solution.
654 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
655 bsym
= raa_write(bsym
, segment
, nglobs
);
656 } else if (sym
->section
!= SHN_ABS
) {
658 * This is a global symbol; so we must add it to the linked
659 * list of global symbols in its section. We'll push it on
660 * the beginning of the list, because it doesn't matter
661 * much which end we put it on and it's easier like this.
663 * In addition, we check the special text for symbol
664 * type and size information.
666 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
667 sects
[sym
->section
- 1]->gsyms
= sym
;
670 int n
= strcspn(special
, " \t");
672 if (!nasm_strnicmp(special
, "function", n
))
673 sym
->type
|= STT_FUNC
;
674 else if (!nasm_strnicmp(special
, "data", n
) ||
675 !nasm_strnicmp(special
, "object", n
))
676 sym
->type
|= STT_OBJECT
;
677 else if (!nasm_strnicmp(special
, "notype", n
))
678 sym
->type
|= STT_NOTYPE
;
680 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
684 while (isspace(*special
))
687 n
= strcspn(special
, " \t");
688 if (!nasm_strnicmp(special
, "default", n
))
689 sym
->other
= STV_DEFAULT
;
690 else if (!nasm_strnicmp(special
, "internal", n
))
691 sym
->other
= STV_INTERNAL
;
692 else if (!nasm_strnicmp(special
, "hidden", n
))
693 sym
->other
= STV_HIDDEN
;
694 else if (!nasm_strnicmp(special
, "protected", n
))
695 sym
->other
= STV_PROTECTED
;
702 struct tokenval tokval
;
705 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
707 while (special
[n
] && isspace(special
[n
]))
710 * We have a size expression; attempt to
714 stdscan_bufptr
= special
+ n
;
715 tokval
.t_type
= TOKEN_INVALID
;
716 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
721 sym
->name
= nasm_strdup(name
);
724 error(ERR_NONFATAL
, "cannot use relocatable"
725 " expression as symbol size");
727 sym
->size
= reloc_value(e
);
729 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
734 sym
->globnum
= nglobs
;
739 if (special
&& !special_used
)
740 error(ERR_NONFATAL
, "no special symbol features supported here");
743 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
746 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
747 sect
->tail
= &r
->next
;
750 r
->address
= sect
->len
;
751 if (segment
== NO_SEG
)
756 for (i
= 0; i
< nsects
; i
++)
757 if (segment
== sects
[i
]->index
)
760 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
768 * This routine deals with ..got and ..sym relocations: the more
769 * complicated kinds. In shared-library writing, some relocations
770 * with respect to global symbols must refer to the precise symbol
771 * rather than referring to an offset from the base of the section
772 * _containing_ the symbol. Such relocations call to this routine,
773 * which searches the symbol list for the symbol in question.
775 * R_386_GOT32 references require the _exact_ symbol address to be
776 * used; R_386_32 references can be at an offset from the symbol.
777 * The boolean argument `exact' tells us this.
779 * Return value is the adjusted value of `addr', having become an
780 * offset from the symbol rather than the section. Should always be
781 * zero when returning from an exact call.
783 * Limitation: if you define two symbols at the same place,
784 * confusion will occur.
786 * Inefficiency: we search, currently, using a linked list which
787 * isn't even necessarily sorted.
789 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
790 int32_t segment
, int64_t offset
,
791 int type
, bool exact
)
795 struct Symbol
*sym
, *sm
;
799 * First look up the segment/offset pair and find a global
800 * symbol corresponding to it. If it's not one of our segments,
801 * then it must be an external symbol, in which case we're fine
802 * doing a normal elf_add_reloc after first sanity-checking
803 * that the offset from the symbol is zero.
806 for (i
= 0; i
< nsects
; i
++)
807 if (segment
== sects
[i
]->index
) {
812 if (exact
&& offset
!= 0)
813 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
814 " for this reference");
816 elf_add_reloc(sect
, segment
, type
);
822 * Find a symbol pointing _exactly_ at this one.
824 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
825 if (sym
->value
== offset
)
829 * Find the nearest symbol below this one.
832 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
833 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
837 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
838 " for this reference");
842 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
843 sect
->tail
= &r
->next
;
846 r
->address
= sect
->len
;
847 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
852 return offset
- sym
->value
;
855 static void elf_out(int32_t segto
, const void *data
,
856 enum out_type type
, uint64_t size
,
857 int32_t segment
, int32_t wrt
)
861 uint8_t mydata
[16], *p
;
863 static struct symlininfo sinfo
;
865 #if defined(DEBUG) && DEBUG>2
866 if (data
) fprintf(stderr
,
867 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
868 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
870 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
871 currentline
, type
, segment
, segto
, size
);
875 * handle absolute-assembly (structure definitions)
877 if (segto
== NO_SEG
) {
878 if (type
!= OUT_RESERVE
)
879 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
885 for (i
= 0; i
< nsects
; i
++)
886 if (segto
== sects
[i
]->index
) {
891 int tempint
; /* ignored */
892 if (segto
!= elf_section_names(".text", 2, &tempint
))
893 error(ERR_PANIC
, "strange segment conditions in ELF driver");
895 s
= sects
[nsects
- 1];
899 /* invoke current debug_output routine */
900 if (of_elf64
.current_dfmt
) {
901 sinfo
.offset
= s
->len
;
904 sinfo
.name
= s
->name
;
905 of_elf64
.current_dfmt
->debug_output(TY_DEBUGSYMLIN
, &sinfo
);
907 /* end of debugging stuff */
909 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
910 error(ERR_WARNING
, "attempt to initialize memory in"
911 " BSS section `%s': ignored", s
->name
);
912 if (type
== OUT_REL2ADR
)
914 else if (type
== OUT_REL4ADR
)
920 if (type
== OUT_RESERVE
) {
921 if (s
->type
== SHT_PROGBITS
) {
922 error(ERR_WARNING
, "uninitialized space declared in"
923 " non-BSS section `%s': zeroing", s
->name
);
924 elf_sect_write(s
, NULL
, size
);
927 } else if (type
== OUT_RAWDATA
) {
928 if (segment
!= NO_SEG
)
929 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
930 elf_sect_write(s
, data
, size
);
931 } else if (type
== OUT_ADDRESS
) {
933 addr
= *(int64_t *)data
;
934 if (segment
!= NO_SEG
) {
936 error(ERR_NONFATAL
, "ELF format does not support"
937 " segment base references");
942 elf_add_reloc(s
, segment
, R_X86_64_16
);
945 elf_add_reloc(s
, segment
, R_X86_64_32
);
948 elf_add_reloc(s
, segment
, R_X86_64_64
);
951 error(ERR_PANIC
, "internal error elf64-hpa-871");
954 } else if (wrt
== elf_gotpc_sect
+ 1) {
956 * The user will supply GOT relative to $$. ELF
957 * will let us have GOT relative to $. So we
958 * need to fix up the data item by $-$$.
961 elf_add_reloc(s
, segment
, R_X86_64_GOTPCREL
);
962 } else if (wrt
== elf_gotoff_sect
+ 1) {
963 elf_add_reloc(s
, segment
, R_X86_64_GOTTPOFF
);
964 } else if (wrt
== elf_got_sect
+ 1) {
965 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
966 R_X86_64_GOT32
, true);
967 } else if (wrt
== elf_sym_sect
+ 1) {
971 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
975 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
979 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
983 error(ERR_PANIC
, "internal error elf64-hpa-903");
986 } else if (wrt
== elf_plt_sect
+ 1) {
987 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
988 "relative PLT references");
990 error(ERR_NONFATAL
, "ELF format does not support this"
992 wrt
= NO_SEG
; /* we can at least _try_ to continue */
1000 if (size
!= 8 && size
!= 4 && segment
!= NO_SEG
) {
1002 "Unsupported non-64-bit ELF relocation");
1004 if (size
== 4) WRITELONG(p
, addr
);
1005 else WRITEDLONG(p
, (int64_t)addr
);
1007 elf_sect_write(s
, mydata
, size
);
1008 } else if (type
== OUT_REL2ADR
) {
1009 if (segment
== segto
)
1010 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
1011 if (segment
!= NO_SEG
&& segment
% 2) {
1012 error(ERR_NONFATAL
, "ELF format does not support"
1013 " segment base references");
1015 if (wrt
== NO_SEG
) {
1016 elf_add_reloc(s
, segment
, R_X86_64_PC16
);
1019 "Unsupported non-32-bit ELF relocation [2]");
1023 WRITESHORT(p
, *(int64_t *)data
- size
);
1024 elf_sect_write(s
, mydata
, 2L);
1025 } else if (type
== OUT_REL4ADR
) {
1026 if (segment
== segto
)
1027 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
1028 if (segment
!= NO_SEG
&& segment
% 2) {
1029 error(ERR_NONFATAL
, "ELF format does not support"
1030 " segment base references");
1032 if (wrt
== NO_SEG
) {
1033 elf_add_reloc(s
, segment
, R_X86_64_PC32
);
1034 } else if (wrt
== elf_plt_sect
+ 1) {
1035 elf_add_reloc(s
, segment
, R_X86_64_PLT32
);
1036 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1037 wrt
== elf_gotoff_sect
+ 1 ||
1038 wrt
== elf_got_sect
+ 1) {
1039 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
1040 "relative GOT references");
1042 error(ERR_NONFATAL
, "ELF format does not support this"
1044 wrt
= NO_SEG
; /* we can at least _try_ to continue */
1048 WRITELONG(p
, *(int64_t *)data
- size
);
1049 elf_sect_write(s
, mydata
, 4L);
1053 static void elf_write(void)
1063 int32_t symtablen
, symtablocal
;
1066 * Work out how many sections we will have. We have SHN_UNDEF,
1067 * then the flexible user sections, then the four fixed
1068 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1069 * then optionally relocation sections for the user sections.
1071 if (of_elf64
.current_dfmt
== &df_stabs
)
1073 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1076 nsections
= 5; /* SHN_UNDEF and the fixed ones */
1078 add_sectname("", ".comment");
1079 add_sectname("", ".shstrtab");
1080 add_sectname("", ".symtab");
1081 add_sectname("", ".strtab");
1082 for (i
= 0; i
< nsects
; i
++) {
1083 nsections
++; /* for the section itself */
1084 if (sects
[i
]->head
) {
1085 nsections
++; /* for its relocations */
1086 add_sectname(".rela", sects
[i
]->name
);
1090 if (of_elf64
.current_dfmt
== &df_stabs
) {
1091 /* in case the debug information is wanted, just add these three sections... */
1092 add_sectname("", ".stab");
1093 add_sectname("", ".stabstr");
1094 add_sectname(".rel", ".stab");
1097 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1098 /* the dwarf debug standard specifies the following ten sections,
1099 not all of which are currently implemented,
1100 although all of them are defined. */
1101 #define debug_aranges (int64_t) (nsections-10)
1102 #define debug_info (int64_t) (nsections-7)
1103 #define debug_abbrev (int64_t) (nsections-5)
1104 #define debug_line (int64_t) (nsections-4)
1105 add_sectname("", ".debug_aranges");
1106 add_sectname(".rela", ".debug_aranges");
1107 add_sectname("", ".debug_pubnames");
1108 add_sectname("", ".debug_info");
1109 add_sectname(".rela", ".debug_info");
1110 add_sectname("", ".debug_abbrev");
1111 add_sectname("", ".debug_line");
1112 add_sectname(".rela", ".debug_line");
1113 add_sectname("", ".debug_frame");
1114 add_sectname("", ".debug_loc");
1122 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1125 * Output the ELF header.
1127 fwrite("\177ELF\2\1\1", 7, 1, elffp
);
1128 fputc(elf_osabi
, elffp
);
1129 fputc(elf_abiver
, elffp
);
1130 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp
);
1131 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1132 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1133 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1134 fwriteint64_t(0L, elffp
); /* no entry point */
1135 fwriteint64_t(0L, elffp
); /* no program header table */
1136 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1137 * ELF header plus alignment */
1138 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1139 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1140 fwriteint16_t(0, elffp
); /* no program header table, again */
1141 fwriteint16_t(0, elffp
); /* still no program header table */
1142 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1143 fwriteint16_t(nsections
, elffp
); /* number of sections */
1144 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1145 * section header table */
1148 * Build the symbol table and relocation tables.
1150 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1151 for (i
= 0; i
< nsects
; i
++)
1153 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1157 * Now output the section header table.
1160 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1161 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1164 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1165 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1166 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1168 for (i
= 0; i
< nsects
; i
++) {
1169 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1170 (sects
[i
]->type
== SHT_PROGBITS
?
1171 sects
[i
]->data
: NULL
), true,
1172 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1174 scount
++; /* ditto */
1176 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1177 scount
++; /* ditto */
1179 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1180 scount
++; /* ditto */
1182 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 24); /* .symtab */
1183 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1185 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1186 for (i
= 0; i
< nsects
; i
++)
1187 if (sects
[i
]->head
) {
1189 elf_section_header(p
- shstrtab
,SHT_RELA
, 0, sects
[i
]->rel
, true,
1190 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 24);
1192 if (of_elf64
.current_dfmt
== &df_stabs
) {
1193 /* for debugging information, create the last three sections
1194 which are the .stab , .stabstr and .rel.stab sections respectively */
1196 /* this function call creates the stab sections in memory */
1199 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1201 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1202 nsections
- 2, 0, 4, 12);
1205 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1206 stabstrlen
, 0, 0, 4, 0);
1209 /* link -> symtable info -> section to refer to */
1210 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1211 stabrellen
, symtabsection
, nsections
- 3, 4,
1215 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1216 /* for dwarf debugging information, create the ten dwarf sections */
1218 /* this function call creates the dwarf sections in memory */
1222 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1223 arangeslen
, 0, 0, 1, 0);
1225 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1226 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1228 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1229 pubnameslen
, 0, 0, 1, 0);
1231 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1232 infolen
, 0, 0, 1, 0);
1234 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1235 inforellen
, symtabsection
, debug_info
, 1, 24);
1237 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1238 abbrevlen
, 0, 0, 1, 0);
1240 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1241 linelen
, 0, 0, 1, 0);
1243 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1244 linerellen
, symtabsection
, debug_line
, 1, 24);
1246 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1247 framelen
, 0, 0, 8, 0);
1249 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1250 loclen
, 0, 0, 1, 0);
1253 fwrite(align_str
, align
, 1, elffp
);
1256 * Now output the sections.
1258 elf_write_sections();
1260 nasm_free(elf_sects
);
1264 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1266 struct SAA
*s
= saa_init(1L);
1268 uint8_t entry
[24], *p
;
1274 * First, an all-zeros entry, required by the ELF spec.
1276 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1281 * Next, an entry for the file name.
1284 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1285 WRITESHORT(p
, STT_FILE
); /* type FILE */
1286 WRITESHORT(p
, SHN_ABS
);
1287 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1288 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1289 saa_wbytes(s
, entry
, 24L);
1294 * Now some standard symbols defining the segments, for relocation
1297 for (i
= 1; i
<= nsects
; i
++) {
1299 WRITELONG(p
, 0); /* no symbol name */
1300 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1301 WRITESHORT(p
, i
); /* section id */
1302 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1303 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1304 saa_wbytes(s
, entry
, 24L);
1311 * Now the other local symbols.
1314 while ((sym
= saa_rstruct(syms
))) {
1315 if (sym
->type
& SYM_GLOBAL
)
1318 WRITELONG(p
, sym
->strpos
); /* index into symbol string table */
1319 WRITECHAR(p
, sym
->type
); /* type and binding */
1320 WRITECHAR(p
, sym
->other
); /* visibility */
1321 WRITESHORT(p
, sym
->section
); /* index into section header table */
1322 WRITEDLONG(p
, (int64_t)sym
->value
); /* value of symbol */
1323 WRITEDLONG(p
, (int64_t)sym
->size
); /* size of symbol */
1324 saa_wbytes(s
, entry
, 24L);
1329 * dwarf needs symbols for debug sections
1330 * which are relocation targets.
1332 if (of_elf64
.current_dfmt
== &df_dwarf
) {
1333 dwarf_infosym
= *local
;
1335 WRITELONG(p
, 0); /* no symbol name */
1336 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1337 WRITESHORT(p
, debug_info
); /* section id */
1338 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1339 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1340 saa_wbytes(s
, entry
, 24L);
1343 dwarf_abbrevsym
= *local
;
1345 WRITELONG(p
, 0); /* no symbol name */
1346 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1347 WRITESHORT(p
, debug_abbrev
); /* section id */
1348 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1349 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1350 saa_wbytes(s
, entry
, 24L);
1353 dwarf_linesym
= *local
;
1355 WRITELONG(p
, 0); /* no symbol name */
1356 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1357 WRITESHORT(p
, debug_line
); /* section id */
1358 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1359 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1360 saa_wbytes(s
, entry
, 24L);
1366 * Now the global symbols.
1369 while ((sym
= saa_rstruct(syms
))) {
1370 if (!(sym
->type
& SYM_GLOBAL
))
1373 WRITELONG(p
, sym
->strpos
);
1374 WRITECHAR(p
, sym
->type
); /* type and binding */
1375 WRITECHAR(p
, sym
->other
); /* visibility */
1376 WRITESHORT(p
, sym
->section
);
1377 WRITEDLONG(p
, (int64_t)sym
->value
);
1378 WRITEDLONG(p
, (int64_t)sym
->size
);
1379 saa_wbytes(s
, entry
, 24L);
1386 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1389 uint8_t *p
, entry
[24];
1398 int64_t sym
= r
->symbol
;
1400 if (sym
>= GLOBAL_TEMP_BASE
)
1402 if (of_elf64
.current_dfmt
== &df_dwarf
)
1403 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 5) + nlocals
;
1404 else sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1407 WRITEDLONG(p
, r
->address
);
1408 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1409 WRITEDLONG(p
, (uint64_t) 0);
1410 saa_wbytes(s
, entry
, 24L);
1419 static void elf_section_header(int name
, int type
, uint64_t flags
,
1420 void *data
, bool is_saa
, uint64_t datalen
,
1421 int link
, int info
, int align
, int eltsize
)
1423 elf_sects
[elf_nsect
].data
= data
;
1424 elf_sects
[elf_nsect
].len
= datalen
;
1425 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1428 fwriteint32_t((int32_t)name
, elffp
);
1429 fwriteint32_t((int32_t)type
, elffp
);
1430 fwriteint64_t((int64_t)flags
, elffp
);
1431 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1432 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1433 fwriteint64_t(datalen
, elffp
);
1435 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1436 fwriteint32_t((int32_t)link
, elffp
);
1437 fwriteint32_t((int32_t)info
, elffp
);
1438 fwriteint64_t((int64_t)align
, elffp
);
1439 fwriteint64_t((int64_t)eltsize
, elffp
);
1442 static void elf_write_sections(void)
1445 for (i
= 0; i
< elf_nsect
; i
++)
1446 if (elf_sects
[i
].data
) {
1447 int32_t len
= elf_sects
[i
].len
;
1448 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1449 int32_t align
= reallen
- len
;
1450 if (elf_sects
[i
].is_saa
)
1451 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1453 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1454 fwrite(align_str
, align
, 1, elffp
);
1458 static void elf_sect_write(struct Section
*sect
,
1459 const uint8_t *data
, uint64_t len
)
1461 saa_wbytes(sect
->data
, data
, len
);
1465 static int32_t elf_segbase(int32_t segment
)
1470 static int elf_directive(char *directive
, char *value
, int pass
)
1476 if (!strcmp(directive
, "osabi")) {
1478 return 1; /* ignore in pass 2 */
1480 n
= readnum(value
, &err
);
1482 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1485 if (n
< 0 || n
> 255) {
1486 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1492 if ((p
= strchr(value
,',')) == NULL
)
1495 n
= readnum(p
+1, &err
);
1496 if (err
|| n
< 0 || n
> 255) {
1497 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1508 static void elf_filename(char *inname
, char *outname
, efunc error
)
1510 strcpy(elf_module
, inname
);
1511 standard_extension(inname
, outname
, ".o", error
);
1514 static const char *elf_stdmac
[] = {
1515 "%define __SECT__ [section .text]",
1516 "%macro __NASM_CDecl__ 1",
1519 "%macro osabi 1+.nolist",
1524 static int elf_set_info(enum geninfo type
, char **val
)
1530 static struct dfmt df_dwarf
= {
1531 "ELF64 (X86_64) dwarf debug format for Linux",
1541 static struct dfmt df_stabs
= {
1542 "ELF64 (X86_64) stabs debug format for Linux",
1553 struct dfmt
*elf64_debugs_arr
[3] = { &df_stabs
, &df_dwarf
, NULL
};
1555 struct ofmt of_elf64
= {
1556 "ELF64 (x86_64) object files (e.g. Linux)",
1573 /* common debugging routines */
1574 void debug64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1581 void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1591 void debug64_directive(const char *directive
, const char *params
)
1597 void debug64_typevalue(int32_t type
)
1599 int32_t stype
, ssize
;
1600 switch (TYM_TYPE(type
)) {
1639 stype
= STT_SECTION
;
1654 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1655 lastsym
->size
= ssize
;
1656 lastsym
->type
= stype
;
1660 /* stabs debugging routines */
1663 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1666 if (!stabs_filename
) {
1667 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1668 strcpy(stabs_filename
, filename
);
1670 if (strcmp(stabs_filename
, filename
)) {
1671 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1672 in fact, this leak comes in quite handy to maintain a list of files
1673 encountered so far in the symbol lines... */
1675 /* why not nasm_free(stabs_filename); we're done with the old one */
1677 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1678 strcpy(stabs_filename
, filename
);
1682 currentline
= linenumber
;
1686 void stabs64_output(int type
, void *param
)
1688 struct symlininfo
*s
;
1689 struct linelist
*el
;
1690 if (type
== TY_DEBUGSYMLIN
) {
1691 if (debug_immcall
) {
1692 s
= (struct symlininfo
*)param
;
1693 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1694 return; /* line info is only collected for executable sections */
1696 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1697 el
->info
.offset
= s
->offset
;
1698 el
->info
.section
= s
->section
;
1699 el
->info
.name
= s
->name
;
1700 el
->line
= currentline
;
1701 el
->filename
= stabs_filename
;
1704 stabslines
->last
->next
= el
;
1705 stabslines
->last
= el
;
1708 stabslines
->last
= el
;
1715 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1717 WRITELONG(p,n_strx); \
1718 WRITECHAR(p,n_type); \
1719 WRITECHAR(p,n_other); \
1720 WRITESHORT(p,n_desc); \
1721 WRITELONG(p,n_value); \
1724 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1726 void stabs64_generate(void)
1728 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1729 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1733 struct linelist
*ptr
;
1737 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1738 for (i
= 0; i
< numlinestabs
; i
++)
1742 if (numfiles
== 0) {
1743 allfiles
[0] = ptr
->filename
;
1746 for (i
= 0; i
< numfiles
; i
++) {
1747 if (!strcmp(allfiles
[i
], ptr
->filename
))
1750 if (i
>= numfiles
) {
1751 allfiles
[i
] = ptr
->filename
;
1758 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1759 for (i
= 0; i
< numfiles
; i
++) {
1760 fileidx
[i
] = strsize
;
1761 strsize
+= strlen(allfiles
[i
]) + 1;
1764 for (i
= 0; i
< numfiles
; i
++) {
1765 if (!strcmp(allfiles
[i
], elf_module
)) {
1771 /* worst case size of the stab buffer would be:
1772 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1775 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1776 sizeof(struct stabentry
));
1778 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1780 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1783 for (i
= 0; i
< numfiles
; i
++) {
1784 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1788 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1795 /* this is the first stab, its strx points to the filename of the
1796 the source-file, the n_desc field should be set to the number
1799 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1801 /* this is the stab for the main source file */
1802 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1804 /* relocation table entry */
1806 /* Since the symbol table has two entries before */
1807 /* the section symbols, the index in the info.section */
1808 /* member must be adjusted by adding 2 */
1810 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1811 WRITELONG(rptr
, R_X86_64_32
);
1812 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1815 currfile
= mainfileindex
;
1819 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1820 /* oops file has changed... */
1821 for (i
= 0; i
< numfiles
; i
++)
1822 if (!strcmp(allfiles
[i
], ptr
->filename
))
1825 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1829 /* relocation table entry */
1831 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1832 WRITELONG(rptr
, R_X86_64_32
);
1833 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1836 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1839 /* relocation table entry */
1841 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1842 WRITELONG(rptr
, R_X86_64_32
);
1843 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1849 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1851 nasm_free(allfiles
);
1854 stablen
= (sptr
- sbuf
);
1855 stabrellen
= (rptr
- rbuf
);
1861 void stabs64_cleanup(void)
1863 struct linelist
*ptr
, *del
;
1875 nasm_free(stabrelbuf
);
1877 nasm_free(stabstrbuf
);
1879 /* dwarf routines */
1882 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1885 dwarf64_findfile(filename
);
1887 currentline
= linenumber
;
1890 /* called from elf_out with type == TY_DEBUGSYMLIN */
1891 void dwarf64_output(int type
, void *param
)
1893 int ln
, aa
, inx
, maxln
, soc
;
1894 struct symlininfo
*s
;
1899 s
= (struct symlininfo
*)param
;
1900 /* line number info is only gathered for executable sections */
1901 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1903 /* Check if section index has changed */
1904 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1906 dwarf64_findsect(s
->section
);
1908 /* do nothing unless line or file has changed */
1911 ln
= currentline
- dwarf_csect
->line
;
1912 aa
= s
->offset
- dwarf_csect
->offset
;
1913 inx
= dwarf_clist
->line
;
1914 plinep
= dwarf_csect
->psaa
;
1915 /* check for file change */
1916 if (!(inx
== dwarf_csect
->file
))
1918 WSAACHAR(plinep
,workbuf
,DW_LNS_set_file
);
1919 WSAACHAR(plinep
,workbuf
,inx
);
1920 dwarf_csect
->file
= inx
;
1922 /* check for line change */
1925 /* test if in range of special op code */
1926 maxln
= line_base
+ line_range
;
1927 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1928 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1930 WSAACHAR(plinep
,workbuf
,soc
);
1936 WSAACHAR(plinep
,workbuf
,DW_LNS_advance_line
);
1937 saa_wleb128s(plinep
,ln
);
1941 WSAACHAR(plinep
,workbuf
,DW_LNS_advance_pc
);
1942 saa_wleb128u(plinep
,aa
);
1945 dwarf_csect
->line
= currentline
;
1946 dwarf_csect
->offset
= s
->offset
;
1948 /* show change handled */
1954 void dwarf64_generate(void)
1958 struct linelist
*ftentry
;
1959 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1960 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1961 struct sectlist
*psect
;
1962 size_t saalen
, linepoff
, totlen
, highaddr
;
1964 /* write epilogues for each line program range */
1965 /* and build aranges section */
1966 paranges
= saa_init(1L);
1967 parangesrel
= saa_init(1L);
1968 WSAASHORT(paranges
,workbuf
,3); /* dwarf version */
1969 WSAADLONG(parangesrel
,workbuf
, paranges
->datalen
+4);
1970 WSAADLONG(parangesrel
,workbuf
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1971 WSAADLONG(parangesrel
,workbuf
, (uint64_t) 0);
1972 WSAALONG(paranges
,workbuf
,0); /* offset into info */
1973 WSAACHAR(paranges
,workbuf
,8); /* pointer size */
1974 WSAACHAR(paranges
,workbuf
,0); /* not segmented */
1975 WSAALONG(paranges
,workbuf
,0); /* padding */
1976 /* iterate though sectlist entries */
1977 psect
= dwarf_fsect
;
1980 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1982 plinep
= psect
->psaa
;
1983 /* Line Number Program Epilogue */
1984 WSAACHAR(plinep
,workbuf
,2); /* std op 2 */
1985 WSAACHAR(plinep
,workbuf
,(sects
[psect
->section
]->len
)-psect
->offset
);
1986 WSAACHAR(plinep
,workbuf
,DW_LNS_extended_op
);
1987 WSAACHAR(plinep
,workbuf
,1); /* operand length */
1988 WSAACHAR(plinep
,workbuf
,DW_LNE_end_sequence
);
1989 totlen
+= plinep
->datalen
;
1990 /* range table relocation entry */
1991 WSAADLONG(parangesrel
,workbuf
, paranges
->datalen
+ 4);
1992 WSAADLONG(parangesrel
,workbuf
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1993 WSAADLONG(parangesrel
,workbuf
, (uint64_t) 0);
1994 /* range table entry */
1995 WSAADLONG(paranges
,workbuf
,0x0000); /* range start */
1996 WSAADLONG(paranges
,workbuf
,sects
[psect
->section
]->len
); /* range length */
1997 highaddr
+= sects
[psect
->section
]->len
;
1998 /* done with this entry */
1999 psect
= psect
->next
;
2001 WSAADLONG(paranges
,workbuf
,0); /* null address */
2002 WSAADLONG(paranges
,workbuf
,0); /* null length */
2003 saalen
= paranges
->datalen
;
2004 arangeslen
= saalen
+ 4;
2005 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2006 WRITELONG(pbuf
,saalen
); /* initial length */
2007 saa_rnbytes(paranges
, pbuf
, saalen
);
2010 /* build rela.aranges section */
2011 arangesrellen
= saalen
= parangesrel
->datalen
;
2012 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2013 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2014 saa_free(parangesrel
);
2016 /* build pubnames section */
2017 ppubnames
= saa_init(1L);
2018 WSAASHORT(ppubnames
,workbuf
,3); /* dwarf version */
2019 WSAALONG(ppubnames
,workbuf
,0); /* offset into info */
2020 WSAALONG(ppubnames
,workbuf
,0); /* space used in info */
2021 WSAALONG(ppubnames
,workbuf
,0); /* end of list */
2022 saalen
= ppubnames
->datalen
;
2023 pubnameslen
= saalen
+ 4;
2024 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2025 WRITELONG(pbuf
,saalen
); /* initial length */
2026 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2027 saa_free(ppubnames
);
2029 /* build info section */
2030 pinfo
= saa_init(1L);
2031 pinforel
= saa_init(1L);
2032 WSAASHORT(pinfo
,workbuf
,3); /* dwarf version */
2033 WSAADLONG(pinforel
,workbuf
, pinfo
->datalen
+ 4);
2034 WSAADLONG(pinforel
,workbuf
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2035 WSAADLONG(pinforel
,workbuf
, (uint64_t) 0);
2036 WSAALONG(pinfo
,workbuf
,0); /* offset into abbrev */
2037 WSAACHAR(pinfo
,workbuf
,8); /* pointer size */
2038 WSAACHAR(pinfo
,workbuf
,1); /* abbrviation number LEB128u */
2039 WSAADLONG(pinforel
,workbuf
, pinfo
->datalen
+ 4);
2040 WSAADLONG(pinforel
,workbuf
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2041 WSAADLONG(pinforel
,workbuf
, (uint64_t) 0);
2042 WSAADLONG(pinfo
,workbuf
,0); /* DW_AT_low_pc */
2043 WSAADLONG(pinforel
,workbuf
, pinfo
->datalen
+ 4);
2044 WSAADLONG(pinforel
,workbuf
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2045 WSAADLONG(pinforel
,workbuf
, (uint64_t) 0);
2046 WSAADLONG(pinfo
,workbuf
,highaddr
); /* DW_AT_high_pc */
2047 WSAADLONG(pinforel
,workbuf
, pinfo
->datalen
+ 4);
2048 WSAADLONG(pinforel
,workbuf
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2049 WSAADLONG(pinforel
,workbuf
, (uint64_t) 0);
2050 WSAALONG(pinfo
,workbuf
,0); /* DW_AT_stmt_list */
2051 strcpy(workbuf
,elf_module
); /* input file name */
2052 saa_wbytes(pinfo
, workbuf
, (int32_t)(strlen(elf_module
) + 1));
2053 sprintf(workbuf
, "NASM %s", NASM_VER
);
2054 saa_wbytes(pinfo
, workbuf
, (int32_t)(strlen(workbuf
) + 1));
2055 WSAASHORT(pinfo
,workbuf
,DW_LANG_Mips_Assembler
);
2056 WSAACHAR(pinfo
,workbuf
,2); /* abbrviation number LEB128u */
2057 WSAADLONG(pinforel
,workbuf
, pinfo
->datalen
+ 4);
2058 WSAADLONG(pinforel
,workbuf
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2059 WSAADLONG(pinforel
,workbuf
, (uint64_t) 0);
2060 WSAADLONG(pinfo
,workbuf
,0); /* DW_AT_low_pc */
2061 WSAADLONG(pinfo
,workbuf
,0); /* DW_AT_frame_base */
2062 WSAACHAR(pinfo
,workbuf
,0); /* end of entries */
2063 saalen
= pinfo
->datalen
;
2064 infolen
= saalen
+ 4;
2065 infobuf
= pbuf
= nasm_malloc(infolen
);
2066 WRITELONG(pbuf
,saalen
); /* initial length */
2067 saa_rnbytes(pinfo
, pbuf
, saalen
);
2070 /* build rela.info section */
2071 inforellen
= saalen
= pinforel
->datalen
;
2072 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2073 saa_rnbytes(pinforel
, pbuf
, saalen
);
2076 /* build abbrev section */
2077 pabbrev
= saa_init(1L);
2078 WSAACHAR(pabbrev
,workbuf
,1); /* entry number LEB128u */
2079 WSAACHAR(pabbrev
,workbuf
,DW_TAG_compile_unit
); /* tag LEB128u */
2080 WSAACHAR(pabbrev
,workbuf
,1); /* has children */
2081 /* the following attributes and forms are all LEB128u values */
2082 WSAACHAR(pabbrev
,workbuf
,DW_AT_low_pc
);
2083 WSAACHAR(pabbrev
,workbuf
,DW_FORM_addr
);
2084 WSAACHAR(pabbrev
,workbuf
,DW_AT_high_pc
);
2085 WSAACHAR(pabbrev
,workbuf
,DW_FORM_addr
);
2086 WSAACHAR(pabbrev
,workbuf
,DW_AT_stmt_list
);
2087 WSAACHAR(pabbrev
,workbuf
,DW_FORM_data4
);
2088 WSAACHAR(pabbrev
,workbuf
,DW_AT_name
);
2089 WSAACHAR(pabbrev
,workbuf
,DW_FORM_string
);
2090 WSAACHAR(pabbrev
,workbuf
,DW_AT_producer
);
2091 WSAACHAR(pabbrev
,workbuf
,DW_FORM_string
);
2092 WSAACHAR(pabbrev
,workbuf
,DW_AT_language
);
2093 WSAACHAR(pabbrev
,workbuf
,DW_FORM_data2
);
2094 WSAASHORT(pabbrev
,workbuf
,0); /* end of entry */
2095 /* LEB128u usage same as above */
2096 WSAACHAR(pabbrev
,workbuf
,2); /* entry number */
2097 WSAACHAR(pabbrev
,workbuf
,DW_TAG_subprogram
);
2098 WSAACHAR(pabbrev
,workbuf
,0); /* no children */
2099 WSAACHAR(pabbrev
,workbuf
,DW_AT_low_pc
);
2100 WSAACHAR(pabbrev
,workbuf
,DW_FORM_addr
);
2101 WSAACHAR(pabbrev
,workbuf
,DW_AT_frame_base
);
2102 WSAACHAR(pabbrev
,workbuf
,DW_FORM_data4
);
2103 WSAASHORT(pabbrev
,workbuf
,0); /* end of entry */
2104 abbrevlen
= saalen
= pabbrev
->datalen
;
2105 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2106 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2109 /* build line section */
2111 plines
= saa_init(1L);
2112 WSAACHAR(plines
,workbuf
,1); /* Minimum Instruction Length */
2113 WSAACHAR(plines
,workbuf
,1); /* Initial value of 'is_stmt' */
2114 WSAACHAR(plines
,workbuf
,line_base
); /* Line Base */
2115 WSAACHAR(plines
,workbuf
,line_range
); /* Line Range */
2116 WSAACHAR(plines
,workbuf
,opcode_base
); /* Opcode Base */
2117 /* standard opcode lengths (# of LEB128u operands) */
2118 WSAACHAR(plines
,workbuf
,0); /* Std opcode 1 length */
2119 WSAACHAR(plines
,workbuf
,1); /* Std opcode 2 length */
2120 WSAACHAR(plines
,workbuf
,1); /* Std opcode 3 length */
2121 WSAACHAR(plines
,workbuf
,1); /* Std opcode 4 length */
2122 WSAACHAR(plines
,workbuf
,1); /* Std opcode 5 length */
2123 WSAACHAR(plines
,workbuf
,0); /* Std opcode 6 length */
2124 WSAACHAR(plines
,workbuf
,0); /* Std opcode 7 length */
2125 WSAACHAR(plines
,workbuf
,0); /* Std opcode 8 length */
2126 WSAACHAR(plines
,workbuf
,1); /* Std opcode 9 length */
2127 WSAACHAR(plines
,workbuf
,0); /* Std opcode 10 length */
2128 WSAACHAR(plines
,workbuf
,0); /* Std opcode 11 length */
2129 WSAACHAR(plines
,workbuf
,1); /* Std opcode 12 length */
2130 /* Directory Table */
2131 WSAACHAR(plines
,workbuf
,0); /* End of table */
2132 /* File Name Table */
2133 ftentry
= dwarf_flist
;
2134 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2136 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2137 WSAACHAR(plines
,workbuf
,0); /* directory LEB128u */
2138 WSAACHAR(plines
,workbuf
,0); /* time LEB128u */
2139 WSAACHAR(plines
,workbuf
,0); /* size LEB128u */
2140 ftentry
= ftentry
->next
;
2142 WSAACHAR(plines
,workbuf
,0); /* End of table */
2143 linepoff
= plines
->datalen
;
2144 linelen
= linepoff
+ totlen
+ 10;
2145 linebuf
= pbuf
= nasm_malloc(linelen
);
2146 WRITELONG(pbuf
,linelen
-4); /* initial length */
2147 WRITESHORT(pbuf
,3); /* dwarf version */
2148 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2149 /* write line header */
2151 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2154 /* concatonate line program ranges */
2156 plinesrel
= saa_init(1L);
2157 psect
= dwarf_fsect
;
2158 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2160 WSAADLONG(plinesrel
,workbuf
, linepoff
);
2161 WSAADLONG(plinesrel
,workbuf
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2162 WSAADLONG(plinesrel
,workbuf
, (uint64_t) 0);
2163 plinep
= psect
->psaa
;
2164 saalen
= plinep
->datalen
;
2165 saa_rnbytes(plinep
, pbuf
, saalen
);
2169 /* done with this entry */
2170 psect
= psect
->next
;
2174 /* build rela.lines section */
2175 linerellen
=saalen
= plinesrel
->datalen
;
2176 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2177 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2178 saa_free(plinesrel
);
2180 /* build frame section */
2182 framebuf
= pbuf
= nasm_malloc(framelen
);
2183 WRITELONG(pbuf
,framelen
-4); /* initial length */
2185 /* build loc section */
2187 locbuf
= pbuf
= nasm_malloc(loclen
);
2188 WRITEDLONG(pbuf
,0); /* null beginning offset */
2189 WRITEDLONG(pbuf
,0); /* null ending offset */
2192 void dwarf64_cleanup(void)
2195 nasm_free(arangesbuf
);
2197 nasm_free(arangesrelbuf
);
2199 nasm_free(pubnamesbuf
);
2203 nasm_free(inforelbuf
);
2205 nasm_free(abbrevbuf
);
2209 nasm_free(linerelbuf
);
2211 nasm_free(framebuf
);
2215 void dwarf64_findfile(const char * fname
)
2218 struct linelist
*match
;
2220 /* return if fname is current file name */
2221 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2222 /* search for match */
2228 match
= dwarf_flist
;
2229 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2231 if (!(strcmp(fname
, match
->filename
)))
2233 dwarf_clist
= match
;
2238 /* add file name to end of list */
2239 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2241 dwarf_clist
->line
= dwarf_numfiles
;
2242 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2243 strcpy(dwarf_clist
->filename
,fname
);
2244 dwarf_clist
->next
= 0;
2245 /* if first entry */
2248 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2249 dwarf_clist
->last
= 0;
2251 /* chain to previous entry */
2254 dwarf_elist
->next
= dwarf_clist
;
2255 dwarf_elist
= dwarf_clist
;
2260 void dwarf64_findsect(const int index
)
2263 struct sectlist
*match
;
2265 /* return if index is current section index */
2266 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2270 /* search for match */
2276 match
= dwarf_fsect
;
2277 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2279 if ((match
->section
== index
))
2281 dwarf_csect
= match
;
2284 match
= match
->next
;
2287 /* add entry to end of list */
2288 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2290 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2291 dwarf_csect
->line
= 1;
2292 dwarf_csect
->offset
= 0;
2293 dwarf_csect
->file
= 1;
2294 dwarf_csect
->section
= index
;
2295 dwarf_csect
->next
= 0;
2296 /* set relocatable address at start of line program */
2297 WSAACHAR(plinep
,workbuf
,DW_LNS_extended_op
);
2298 WSAACHAR(plinep
,workbuf
,9); /* operand length */
2299 WSAACHAR(plinep
,workbuf
,DW_LNE_set_address
);
2300 WSAADLONG(plinep
,workbuf
,0); /* Start Address */
2301 /* if first entry */
2304 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2305 dwarf_csect
->last
= 0;
2307 /* chain to previous entry */
2310 dwarf_esect
->next
= dwarf_csect
;
2311 dwarf_esect
= dwarf_csect
;