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.
24 /* Definitions in lieu of elf.h */
25 #define SHT_NULL 0 /* Inactive section header */
26 #define SHT_PROGBITS 1 /* Program defined content */
27 #define SHT_RELA 4 /* Relocation entries with addends */
28 #define SHT_NOBITS 8 /* Section requires no space in file */
29 #define SHF_WRITE (1 << 0) /* Writable */
30 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
31 #define SHF_EXECINSTR (1 << 2) /* Executable */
32 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
33 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
34 #define R_X86_64_NONE 0 /* No reloc */
35 #define R_X86_64_64 1 /* Direct 64 bit address */
36 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
37 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
38 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
39 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
40 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
41 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
42 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
43 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
44 #define ET_REL 1 /* Relocatable file */
45 #define EM_X86_64 62 /* AMD x86-64 architecture */
46 #define STT_NOTYPE 0 /* Symbol type is unspecified */
47 #define STT_OBJECT 1 /* Symbol is a data object */
48 #define STT_FUNC 2 /* Symbol is a code object */
49 #define STT_SECTION 3 /* Symbol associated with a section */
50 #define STT_FILE 4 /* Symbol's name is file name */
51 #define STT_COMMON 5 /* Symbol is a common data object */
52 #define STT_TLS 6 /* Symbol is thread-local data object*/
53 #define STT_NUM 7 /* Number of defined types. */
55 /* Definitions in lieu of dwarf.h */
56 #define DW_TAG_compile_unit 0x11
57 #define DW_TAG_subprogram 0x2e
58 #define DW_AT_name 0x03
59 #define DW_AT_stmt_list 0x10
60 #define DW_AT_low_pc 0x11
61 #define DW_AT_high_pc 0x12
62 #define DW_AT_language 0x13
63 #define DW_AT_producer 0x25
64 #define DW_AT_frame_base 0x40
65 #define DW_FORM_addr 0x01
66 #define DW_FORM_data2 0x05
67 #define DW_FORM_data4 0x06
68 #define DW_FORM_string 0x08
69 #define DW_LNS_extended_op 0
70 #define DW_LNS_advance_pc 2
71 #define DW_LNS_advance_line 3
72 #define DW_LNS_set_file 4
73 #define DW_LNE_end_sequence 1
74 #define DW_LNE_set_address 2
75 #define DW_LNE_define_file 3
76 #define DW_LANG_Mips_Assembler 0x8001
78 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
80 typedef uint32_t Elf64_Word
;
81 typedef uint64_t Elf64_Xword
;
82 typedef uint64_t Elf64_Addr
;
83 typedef uint64_t Elf64_Off
;
86 Elf64_Word sh_name
; /* Section name (string tbl index) */
87 Elf64_Word sh_type
; /* Section type */
88 Elf64_Xword sh_flags
; /* Section flags */
89 Elf64_Addr sh_addr
; /* Section virtual addr at execution */
90 Elf64_Off sh_offset
; /* Section file offset */
91 Elf64_Xword sh_size
; /* Section size in bytes */
92 Elf64_Word sh_link
; /* Link to another section */
93 Elf64_Word sh_info
; /* Additional section information */
94 Elf64_Xword sh_addralign
; /* Section alignment */
95 Elf64_Xword sh_entsize
; /* Entry size if section holds table */
104 int64_t address
; /* relative to _start_ of section */
105 int64_t symbol
; /* symbol index */
106 int type
; /* type of relocation */
110 int32_t strpos
; /* string table position of name */
111 int32_t section
; /* section ID of the symbol */
112 int type
; /* symbol type */
113 int other
; /* symbol visibility */
114 int64_t value
; /* address, or COMMON variable align */
115 int32_t size
; /* size of symbol */
116 int32_t globnum
; /* symbol table offset if global */
117 struct Symbol
*next
; /* list of globals in each section */
118 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
119 char *name
; /* used temporarily if in above list */
127 int32_t index
; /* index into sects array */
128 uint32_t type
; /* SHT_PROGBITS or SHT_NOBITS */
129 uint64_t align
; /* alignment: power of two */
130 uint64_t flags
; /* section flags */
134 struct Reloc
*head
, **tail
;
135 struct Symbol
*gsyms
; /* global symbols in section */
138 #define SECT_DELTA 32
139 static struct Section
**sects
;
140 static int nsects
, sectlen
;
142 #define SHSTR_DELTA 256
143 static char *shstrtab
;
144 static int shstrtablen
, shstrtabsize
;
146 static struct SAA
*syms
;
147 static uint32_t nlocals
, nglobs
;
149 static int32_t def_seg
;
151 static struct RAA
*bsym
;
153 static struct SAA
*strs
;
154 static uint32_t strslen
;
158 static evalfunc evaluate
;
160 static struct Symbol
*fwds
;
162 static char elf_module
[FILENAME_MAX
];
164 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
165 static uint8_t elf_abiver
= 0; /* Current ABI version */
167 extern struct ofmt of_elf64
;
171 #define SYM_GLOBAL 0x10
173 #define STV_DEFAULT 0
174 #define STV_INTERNAL 1
176 #define STV_PROTECTED 3
178 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
180 #define SEG_ALIGN 16 /* alignment of sections in file */
181 #define SEG_ALIGN_1 (SEG_ALIGN-1)
183 #define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
185 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
187 static struct ELF_SECTDATA
{
192 static int elf_nsect
, nsections
;
193 static int64_t elf_foffs
;
195 static void elf_write(void);
196 static void elf_sect_write(struct Section
*, const uint8_t *,
198 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
200 static void elf_write_sections(void);
201 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
202 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
203 static void add_sectname(char *, char *);
205 /* type values for stabs debugging sections */
206 #define N_SO 0x64 /* ID for main source file */
207 #define N_SOL 0x84 /* ID for sub-source file */
208 #define N_BINCL 0x82 /* not currently used */
209 #define N_EINCL 0xA2 /* not currently used */
226 int section
; /* index into sects[] */
227 int segto
; /* internal section number */
228 char *name
; /* shallow-copied pointer of section name */
232 struct symlininfo info
;
235 struct linelist
*next
;
236 struct linelist
*last
;
245 struct sectlist
*next
;
246 struct sectlist
*last
;
249 /* common debug variables */
250 static int currentline
= 1;
251 static int debug_immcall
= 0;
253 /* stabs debug variables */
254 static struct linelist
*stabslines
= 0;
255 static int numlinestabs
= 0;
256 static char *stabs_filename
= 0;
257 static int symtabsection
;
258 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
259 static int stablen
, stabstrlen
, stabrellen
;
261 /* dwarf debug variables */
262 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
263 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
264 static int dwarf_numfiles
= 0, dwarf_nsections
;
265 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
266 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
267 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
268 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
269 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
270 static int64_t dwarf_infosym
, dwarf_abbrevsym
, dwarf_linesym
;
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
&& !nasm_isspace(*p
))
439 flags_and
= flags_or
= type
= align
= 0;
441 while (*p
&& nasm_isspace(*p
))
445 while (*p
&& !nasm_isspace(*p
))
449 while (*p
&& nasm_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
&& !nasm_isspace(*p
))
567 while (*p
&& nasm_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 (nasm_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
] && nasm_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 */
1219 if (dwarf_fsect
) dwarf64_generate();
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 extern macros_t elf_stdmac
[];
1516 static int elf_set_info(enum geninfo type
, char **val
)
1522 static struct dfmt df_dwarf
= {
1523 "ELF64 (X86_64) dwarf debug format for Linux",
1533 static struct dfmt df_stabs
= {
1534 "ELF64 (X86_64) stabs debug format for Linux",
1545 struct dfmt
*elf64_debugs_arr
[3] = { &df_stabs
, &df_dwarf
, NULL
};
1547 struct ofmt of_elf64
= {
1548 "ELF64 (x86_64) object files (e.g. Linux)",
1565 /* common debugging routines */
1566 void debug64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1573 void debug64_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1583 void debug64_directive(const char *directive
, const char *params
)
1589 void debug64_typevalue(int32_t type
)
1591 int32_t stype
, ssize
;
1592 switch (TYM_TYPE(type
)) {
1631 stype
= STT_SECTION
;
1646 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1647 lastsym
->size
= ssize
;
1648 lastsym
->type
= stype
;
1652 /* stabs debugging routines */
1655 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1658 if (!stabs_filename
) {
1659 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1660 strcpy(stabs_filename
, filename
);
1662 if (strcmp(stabs_filename
, filename
)) {
1663 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1664 in fact, this leak comes in quite handy to maintain a list of files
1665 encountered so far in the symbol lines... */
1667 /* why not nasm_free(stabs_filename); we're done with the old one */
1669 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1670 strcpy(stabs_filename
, filename
);
1674 currentline
= linenumber
;
1678 void stabs64_output(int type
, void *param
)
1680 struct symlininfo
*s
;
1681 struct linelist
*el
;
1682 if (type
== TY_DEBUGSYMLIN
) {
1683 if (debug_immcall
) {
1684 s
= (struct symlininfo
*)param
;
1685 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1686 return; /* line info is only collected for executable sections */
1688 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1689 el
->info
.offset
= s
->offset
;
1690 el
->info
.section
= s
->section
;
1691 el
->info
.name
= s
->name
;
1692 el
->line
= currentline
;
1693 el
->filename
= stabs_filename
;
1696 stabslines
->last
->next
= el
;
1697 stabslines
->last
= el
;
1700 stabslines
->last
= el
;
1707 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1709 WRITELONG(p,n_strx); \
1710 WRITECHAR(p,n_type); \
1711 WRITECHAR(p,n_other); \
1712 WRITESHORT(p,n_desc); \
1713 WRITELONG(p,n_value); \
1716 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1718 void stabs64_generate(void)
1720 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1721 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1725 struct linelist
*ptr
;
1729 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1730 for (i
= 0; i
< numlinestabs
; i
++)
1734 if (numfiles
== 0) {
1735 allfiles
[0] = ptr
->filename
;
1738 for (i
= 0; i
< numfiles
; i
++) {
1739 if (!strcmp(allfiles
[i
], ptr
->filename
))
1742 if (i
>= numfiles
) {
1743 allfiles
[i
] = ptr
->filename
;
1750 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1751 for (i
= 0; i
< numfiles
; i
++) {
1752 fileidx
[i
] = strsize
;
1753 strsize
+= strlen(allfiles
[i
]) + 1;
1756 for (i
= 0; i
< numfiles
; i
++) {
1757 if (!strcmp(allfiles
[i
], elf_module
)) {
1763 /* worst case size of the stab buffer would be:
1764 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1767 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1768 sizeof(struct stabentry
));
1770 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1772 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1775 for (i
= 0; i
< numfiles
; i
++) {
1776 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1780 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1787 /* this is the first stab, its strx points to the filename of the
1788 the source-file, the n_desc field should be set to the number
1791 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1793 /* this is the stab for the main source file */
1794 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1796 /* relocation table entry */
1798 /* Since the symbol table has two entries before */
1799 /* the section symbols, the index in the info.section */
1800 /* member must be adjusted by adding 2 */
1802 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1803 WRITELONG(rptr
, R_X86_64_32
);
1804 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1807 currfile
= mainfileindex
;
1811 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1812 /* oops file has changed... */
1813 for (i
= 0; i
< numfiles
; i
++)
1814 if (!strcmp(allfiles
[i
], ptr
->filename
))
1817 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1821 /* relocation table entry */
1823 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1824 WRITELONG(rptr
, R_X86_64_32
);
1825 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1828 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1831 /* relocation table entry */
1833 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1834 WRITELONG(rptr
, R_X86_64_32
);
1835 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1841 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1843 nasm_free(allfiles
);
1846 stablen
= (sptr
- sbuf
);
1847 stabrellen
= (rptr
- rbuf
);
1853 void stabs64_cleanup(void)
1855 struct linelist
*ptr
, *del
;
1867 nasm_free(stabrelbuf
);
1869 nasm_free(stabstrbuf
);
1871 /* dwarf routines */
1874 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1877 dwarf64_findfile(filename
);
1879 currentline
= linenumber
;
1882 /* called from elf_out with type == TY_DEBUGSYMLIN */
1883 void dwarf64_output(int type
, void *param
)
1885 int ln
, aa
, inx
, maxln
, soc
;
1886 struct symlininfo
*s
;
1891 s
= (struct symlininfo
*)param
;
1892 /* line number info is only gathered for executable sections */
1893 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1895 /* Check if section index has changed */
1896 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
1898 dwarf64_findsect(s
->section
);
1900 /* do nothing unless line or file has changed */
1903 ln
= currentline
- dwarf_csect
->line
;
1904 aa
= s
->offset
- dwarf_csect
->offset
;
1905 inx
= dwarf_clist
->line
;
1906 plinep
= dwarf_csect
->psaa
;
1907 /* check for file change */
1908 if (!(inx
== dwarf_csect
->file
))
1910 saa_write8(plinep
,DW_LNS_set_file
);
1911 saa_write8(plinep
,inx
);
1912 dwarf_csect
->file
= inx
;
1914 /* check for line change */
1917 /* test if in range of special op code */
1918 maxln
= line_base
+ line_range
;
1919 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
1920 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
1922 saa_write8(plinep
,soc
);
1928 saa_write8(plinep
,DW_LNS_advance_line
);
1929 saa_wleb128s(plinep
,ln
);
1933 saa_write8(plinep
,DW_LNS_advance_pc
);
1934 saa_wleb128u(plinep
,aa
);
1937 dwarf_csect
->line
= currentline
;
1938 dwarf_csect
->offset
= s
->offset
;
1940 /* show change handled */
1946 void dwarf64_generate(void)
1948 static const char nasm_signature
[] = "NASM " NASM_VER
;
1951 struct linelist
*ftentry
;
1952 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
1953 struct SAA
*parangesrel
, *plinesrel
, *pinforel
;
1954 struct sectlist
*psect
;
1955 size_t saalen
, linepoff
, totlen
, highaddr
;
1957 /* write epilogues for each line program range */
1958 /* and build aranges section */
1959 paranges
= saa_init(1L);
1960 parangesrel
= saa_init(1L);
1961 saa_write16(paranges
,3); /* dwarf version */
1962 saa_write64(parangesrel
, paranges
->datalen
+4);
1963 saa_write64(parangesrel
, (dwarf_infosym
<< 32) + R_X86_64_32
); /* reloc to info */
1964 saa_write64(parangesrel
, 0);
1965 saa_write32(paranges
,0); /* offset into info */
1966 saa_write8(paranges
,8); /* pointer size */
1967 saa_write8(paranges
,0); /* not segmented */
1968 saa_write32(paranges
,0); /* padding */
1969 /* iterate though sectlist entries */
1970 psect
= dwarf_fsect
;
1973 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
1975 plinep
= psect
->psaa
;
1976 /* Line Number Program Epilogue */
1977 saa_write8(plinep
,2); /* std op 2 */
1978 saa_write8(plinep
,(sects
[psect
->section
]->len
)-psect
->offset
);
1979 saa_write8(plinep
,DW_LNS_extended_op
);
1980 saa_write8(plinep
,1); /* operand length */
1981 saa_write8(plinep
,DW_LNE_end_sequence
);
1982 totlen
+= plinep
->datalen
;
1983 /* range table relocation entry */
1984 saa_write64(parangesrel
, paranges
->datalen
+ 4);
1985 saa_write64(parangesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
1986 saa_write64(parangesrel
, (uint64_t) 0);
1987 /* range table entry */
1988 saa_write64(paranges
,0x0000); /* range start */
1989 saa_write64(paranges
,sects
[psect
->section
]->len
); /* range length */
1990 highaddr
+= sects
[psect
->section
]->len
;
1991 /* done with this entry */
1992 psect
= psect
->next
;
1994 saa_write64(paranges
,0); /* null address */
1995 saa_write64(paranges
,0); /* null length */
1996 saalen
= paranges
->datalen
;
1997 arangeslen
= saalen
+ 4;
1998 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
1999 WRITELONG(pbuf
,saalen
); /* initial length */
2000 saa_rnbytes(paranges
, pbuf
, saalen
);
2003 /* build rela.aranges section */
2004 arangesrellen
= saalen
= parangesrel
->datalen
;
2005 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2006 saa_rnbytes(parangesrel
, pbuf
, saalen
);
2007 saa_free(parangesrel
);
2009 /* build pubnames section */
2010 ppubnames
= saa_init(1L);
2011 saa_write16(ppubnames
,3); /* dwarf version */
2012 saa_write32(ppubnames
,0); /* offset into info */
2013 saa_write32(ppubnames
,0); /* space used in info */
2014 saa_write32(ppubnames
,0); /* end of list */
2015 saalen
= ppubnames
->datalen
;
2016 pubnameslen
= saalen
+ 4;
2017 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2018 WRITELONG(pbuf
,saalen
); /* initial length */
2019 saa_rnbytes(ppubnames
, pbuf
, saalen
);
2020 saa_free(ppubnames
);
2022 /* build info section */
2023 pinfo
= saa_init(1L);
2024 pinforel
= saa_init(1L);
2025 saa_write16(pinfo
,3); /* dwarf version */
2026 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2027 saa_write64(pinforel
, (dwarf_abbrevsym
<< 32) + R_X86_64_32
); /* reloc to abbrev */
2028 saa_write64(pinforel
, 0);
2029 saa_write32(pinfo
,0); /* offset into abbrev */
2030 saa_write8(pinfo
,8); /* pointer size */
2031 saa_write8(pinfo
,1); /* abbrviation number LEB128u */
2032 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2033 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2034 saa_write64(pinforel
, 0);
2035 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2036 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2037 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2038 saa_write64(pinforel
, 0);
2039 saa_write64(pinfo
,highaddr
); /* DW_AT_high_pc */
2040 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2041 saa_write64(pinforel
, (dwarf_linesym
<< 32) + R_X86_64_32
); /* reloc to line */
2042 saa_write64(pinforel
, 0);
2043 saa_write32(pinfo
,0); /* DW_AT_stmt_list */
2044 saa_wbytes(pinfo
, elf_module
, strlen(elf_module
)+1);
2045 saa_wbytes(pinfo
, nasm_signature
, strlen(nasm_signature
)+1);
2046 saa_write16(pinfo
,DW_LANG_Mips_Assembler
);
2047 saa_write8(pinfo
,2); /* abbrviation number LEB128u */
2048 saa_write64(pinforel
, pinfo
->datalen
+ 4);
2049 saa_write64(pinforel
, ((uint64_t)(dwarf_fsect
->section
+ 2) << 32) + R_X86_64_64
);
2050 saa_write64(pinforel
, 0);
2051 saa_write64(pinfo
,0); /* DW_AT_low_pc */
2052 saa_write64(pinfo
,0); /* DW_AT_frame_base */
2053 saa_write8(pinfo
,0); /* end of entries */
2054 saalen
= pinfo
->datalen
;
2055 infolen
= saalen
+ 4;
2056 infobuf
= pbuf
= nasm_malloc(infolen
);
2057 WRITELONG(pbuf
,saalen
); /* initial length */
2058 saa_rnbytes(pinfo
, pbuf
, saalen
);
2061 /* build rela.info section */
2062 inforellen
= saalen
= pinforel
->datalen
;
2063 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2064 saa_rnbytes(pinforel
, pbuf
, saalen
);
2067 /* build abbrev section */
2068 pabbrev
= saa_init(1L);
2069 saa_write8(pabbrev
,1); /* entry number LEB128u */
2070 saa_write8(pabbrev
,DW_TAG_compile_unit
); /* tag LEB128u */
2071 saa_write8(pabbrev
,1); /* has children */
2072 /* the following attributes and forms are all LEB128u values */
2073 saa_write8(pabbrev
,DW_AT_low_pc
);
2074 saa_write8(pabbrev
,DW_FORM_addr
);
2075 saa_write8(pabbrev
,DW_AT_high_pc
);
2076 saa_write8(pabbrev
,DW_FORM_addr
);
2077 saa_write8(pabbrev
,DW_AT_stmt_list
);
2078 saa_write8(pabbrev
,DW_FORM_data4
);
2079 saa_write8(pabbrev
,DW_AT_name
);
2080 saa_write8(pabbrev
,DW_FORM_string
);
2081 saa_write8(pabbrev
,DW_AT_producer
);
2082 saa_write8(pabbrev
,DW_FORM_string
);
2083 saa_write8(pabbrev
,DW_AT_language
);
2084 saa_write8(pabbrev
,DW_FORM_data2
);
2085 saa_write16(pabbrev
,0); /* end of entry */
2086 /* LEB128u usage same as above */
2087 saa_write8(pabbrev
,2); /* entry number */
2088 saa_write8(pabbrev
,DW_TAG_subprogram
);
2089 saa_write8(pabbrev
,0); /* no children */
2090 saa_write8(pabbrev
,DW_AT_low_pc
);
2091 saa_write8(pabbrev
,DW_FORM_addr
);
2092 saa_write8(pabbrev
,DW_AT_frame_base
);
2093 saa_write8(pabbrev
,DW_FORM_data4
);
2094 saa_write16(pabbrev
,0); /* end of entry */
2095 abbrevlen
= saalen
= pabbrev
->datalen
;
2096 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2097 saa_rnbytes(pabbrev
, pbuf
, saalen
);
2100 /* build line section */
2102 plines
= saa_init(1L);
2103 saa_write8(plines
,1); /* Minimum Instruction Length */
2104 saa_write8(plines
,1); /* Initial value of 'is_stmt' */
2105 saa_write8(plines
,line_base
); /* Line Base */
2106 saa_write8(plines
,line_range
); /* Line Range */
2107 saa_write8(plines
,opcode_base
); /* Opcode Base */
2108 /* standard opcode lengths (# of LEB128u operands) */
2109 saa_write8(plines
,0); /* Std opcode 1 length */
2110 saa_write8(plines
,1); /* Std opcode 2 length */
2111 saa_write8(plines
,1); /* Std opcode 3 length */
2112 saa_write8(plines
,1); /* Std opcode 4 length */
2113 saa_write8(plines
,1); /* Std opcode 5 length */
2114 saa_write8(plines
,0); /* Std opcode 6 length */
2115 saa_write8(plines
,0); /* Std opcode 7 length */
2116 saa_write8(plines
,0); /* Std opcode 8 length */
2117 saa_write8(plines
,1); /* Std opcode 9 length */
2118 saa_write8(plines
,0); /* Std opcode 10 length */
2119 saa_write8(plines
,0); /* Std opcode 11 length */
2120 saa_write8(plines
,1); /* Std opcode 12 length */
2121 /* Directory Table */
2122 saa_write8(plines
,0); /* End of table */
2123 /* File Name Table */
2124 ftentry
= dwarf_flist
;
2125 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2127 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2128 saa_write8(plines
,0); /* directory LEB128u */
2129 saa_write8(plines
,0); /* time LEB128u */
2130 saa_write8(plines
,0); /* size LEB128u */
2131 ftentry
= ftentry
->next
;
2133 saa_write8(plines
,0); /* End of table */
2134 linepoff
= plines
->datalen
;
2135 linelen
= linepoff
+ totlen
+ 10;
2136 linebuf
= pbuf
= nasm_malloc(linelen
);
2137 WRITELONG(pbuf
,linelen
-4); /* initial length */
2138 WRITESHORT(pbuf
,3); /* dwarf version */
2139 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2140 /* write line header */
2142 saa_rnbytes(plines
, pbuf
, saalen
); /* read a given no. of bytes */
2145 /* concatonate line program ranges */
2147 plinesrel
= saa_init(1L);
2148 psect
= dwarf_fsect
;
2149 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2151 saa_write64(plinesrel
, linepoff
);
2152 saa_write64(plinesrel
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2153 saa_write64(plinesrel
, (uint64_t) 0);
2154 plinep
= psect
->psaa
;
2155 saalen
= plinep
->datalen
;
2156 saa_rnbytes(plinep
, pbuf
, saalen
);
2160 /* done with this entry */
2161 psect
= psect
->next
;
2165 /* build rela.lines section */
2166 linerellen
=saalen
= plinesrel
->datalen
;
2167 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2168 saa_rnbytes(plinesrel
, pbuf
, saalen
);
2169 saa_free(plinesrel
);
2171 /* build frame section */
2173 framebuf
= pbuf
= nasm_malloc(framelen
);
2174 WRITELONG(pbuf
,framelen
-4); /* initial length */
2176 /* build loc section */
2178 locbuf
= pbuf
= nasm_malloc(loclen
);
2179 WRITEDLONG(pbuf
,0); /* null beginning offset */
2180 WRITEDLONG(pbuf
,0); /* null ending offset */
2183 void dwarf64_cleanup(void)
2186 nasm_free(arangesbuf
);
2188 nasm_free(arangesrelbuf
);
2190 nasm_free(pubnamesbuf
);
2194 nasm_free(inforelbuf
);
2196 nasm_free(abbrevbuf
);
2200 nasm_free(linerelbuf
);
2202 nasm_free(framebuf
);
2206 void dwarf64_findfile(const char * fname
)
2209 struct linelist
*match
;
2211 /* return if fname is current file name */
2212 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2213 /* search for match */
2219 match
= dwarf_flist
;
2220 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2222 if (!(strcmp(fname
, match
->filename
)))
2224 dwarf_clist
= match
;
2229 /* add file name to end of list */
2230 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2232 dwarf_clist
->line
= dwarf_numfiles
;
2233 dwarf_clist
->filename
= nasm_malloc(strlen(fname
) + 1);
2234 strcpy(dwarf_clist
->filename
,fname
);
2235 dwarf_clist
->next
= 0;
2236 /* if first entry */
2239 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2240 dwarf_clist
->last
= 0;
2242 /* chain to previous entry */
2245 dwarf_elist
->next
= dwarf_clist
;
2246 dwarf_elist
= dwarf_clist
;
2251 void dwarf64_findsect(const int index
)
2254 struct sectlist
*match
;
2256 /* return if index is current section index */
2257 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2261 /* search for match */
2267 match
= dwarf_fsect
;
2268 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2270 if ((match
->section
== index
))
2272 dwarf_csect
= match
;
2275 match
= match
->next
;
2278 /* add entry to end of list */
2279 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2281 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2282 dwarf_csect
->line
= 1;
2283 dwarf_csect
->offset
= 0;
2284 dwarf_csect
->file
= 1;
2285 dwarf_csect
->section
= index
;
2286 dwarf_csect
->next
= 0;
2287 /* set relocatable address at start of line program */
2288 saa_write8(plinep
,DW_LNS_extended_op
);
2289 saa_write8(plinep
,9); /* operand length */
2290 saa_write8(plinep
,DW_LNE_set_address
);
2291 saa_write64(plinep
,0); /* Start Address */
2292 /* if first entry */
2295 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2296 dwarf_csect
->last
= 0;
2298 /* chain to previous entry */
2301 dwarf_esect
->next
= dwarf_csect
;
2302 dwarf_esect
= dwarf_csect
;