1 /* outelf.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.
22 /* Definitions in lieu of elf.h */
23 #define SHT_NULL 0 /* Inactive section header */
24 #define SHT_PROGBITS 1 /* Program defined content */
25 #define SHT_RELA 4 /* Relocation entries with addends */
26 #define SHT_NOBITS 8 /* Section requires no space in file */
27 #define SHF_WRITE (1 << 0) /* Writable */
28 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
29 #define SHF_EXECINSTR (1 << 2) /* Executable */
30 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
31 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
32 #define R_X86_64_NONE 0 /* No reloc */
33 #define R_X86_64_64 1 /* Direct 64 bit address */
34 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
35 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
36 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
37 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
38 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
39 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
40 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
41 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
42 #define ET_REL 1 /* Relocatable file */
43 #define EM_X86_64 62 /* AMD x86-64 architecture */
44 #define STT_NOTYPE 0 /* Symbol type is unspecified */
45 #define STT_OBJECT 1 /* Symbol is a data object */
46 #define STT_FUNC 2 /* Symbol is a code object */
47 #define STT_SECTION 3 /* Symbol associated with a section */
48 #define STT_FILE 4 /* Symbol's name is file name */
49 #define STT_COMMON 5 /* Symbol is a common data object */
50 #define STT_TLS 6 /* Symbol is thread-local data object*/
51 #define STT_NUM 7 /* Number of defined types. */
53 /* Definitions in lieu of dwarf.h */
54 #define DW_TAG_compile_unit 0x11
55 #define DW_TAG_subprogram 0x2e
56 #define DW_AT_low_pc 0x11
57 #define DW_AT_high_pc 0x12
58 #define DW_AT_frame_base 0x40
59 #define DW_AT_name 0x03
60 #define DW_AT_stmt_list 0x10
61 #define DW_FORM_addr 0x01
62 #define DW_FORM_data4 0x06
63 #define DW_FORM_string 0x08
64 #define DW_LNS_extended_op 0
65 #define DW_LNS_advance_pc 2
66 #define DW_LNS_advance_line 3
67 #define DW_LNS_set_file 4
68 #define DW_LNE_end_sequence 1
69 #define DW_LNE_set_address 2
70 #define DW_LNE_define_file 3
71 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
74 #define WSAACHAR(s,p,v) \
76 *(uint8_t *)(p) = (v); \
77 saa_wbytes(s, p, 1); \
80 #define WSAASHORT(s,p,v) \
82 *(uint16_t *)(p) = (v); \
83 saa_wbytes(s, p, 2); \
86 #define WSAALONG(s,p,v) \
88 *(uint32_t *)(p) = (v); \
89 saa_wbytes(s, p, 4); \
92 #define WSAADLONG(s,p,v) \
94 *(uint64_t *)(p) = (v); \
95 saa_wbytes(s, p, 8); \
98 #define WSAAADDR(a,p,v,s) \
101 memcpy((p), &_v, (s)); \
102 saa_wbytes(a, p, s); \
105 #else /* !X86_MEMORY */
107 #define WSAACHAR(s,p,v) \
109 *(uint8_t *)p = (v); \
110 saa_wbytes(s, p, 1); \
113 #define WSAASHORT(s,p,v) \
116 uint8_t *_p = (uint8_t *)(p); \
119 saa_wbytes(s, _p, 2); \
122 #define WSAALONG(s,p,v) \
125 uint8_t *_p = (uint8_t *)(p); \
130 saa_wbytes(s, _p, 4); \
133 #define WSAADLONG(s,p,v) \
136 uint8_t *_p = (uint8_t *)(p); \
145 saa_wbytes(s, _p, 8); \
148 #define WSAAADDR(a,p,v,s) \
151 uint8_t *_p = (uint8_t *)(p); \
160 saa_wbytes(a, _p, s); \
165 typedef uint32_t Elf64_Word
;
166 typedef uint64_t Elf64_Xword
;
167 typedef uint64_t Elf64_Addr
;
168 typedef uint64_t Elf64_Off
;
171 Elf64_Word sh_name
; /* Section name (string tbl index) */
172 Elf64_Word sh_type
; /* Section type */
173 Elf64_Xword sh_flags
; /* Section flags */
174 Elf64_Addr sh_addr
; /* Section virtual addr at execution */
175 Elf64_Off sh_offset
; /* Section file offset */
176 Elf64_Xword sh_size
; /* Section size in bytes */
177 Elf64_Word sh_link
; /* Link to another section */
178 Elf64_Word sh_info
; /* Additional section information */
179 Elf64_Xword sh_addralign
; /* Section alignment */
180 Elf64_Xword sh_entsize
; /* Entry size if section holds table */
189 int64_t address
; /* relative to _start_ of section */
190 int64_t symbol
; /* symbol index */
191 int type
; /* type of relocation */
195 int32_t strpos
; /* string table position of name */
196 int32_t section
; /* section ID of the symbol */
197 int type
; /* symbol type */
198 int other
; /* symbol visibility */
199 int64_t value
; /* address, or COMMON variable align */
200 int32_t size
; /* size of symbol */
201 int32_t globnum
; /* symbol table offset if global */
202 struct Symbol
*next
; /* list of globals in each section */
203 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
204 char *name
; /* used temporarily if in above list */
212 int32_t index
; /* index into sects array */
213 uint32_t type
; /* SHT_PROGBITS or SHT_NOBITS */
214 uint64_t align
; /* alignment: power of two */
215 uint64_t flags
; /* section flags */
219 struct Reloc
*head
, **tail
;
220 struct Symbol
*gsyms
; /* global symbols in section */
223 #define SECT_DELTA 32
224 static struct Section
**sects
;
225 static int nsects
, sectlen
;
227 #define SHSTR_DELTA 256
228 static char *shstrtab
;
229 static int shstrtablen
, shstrtabsize
;
231 static struct SAA
*syms
;
232 static uint32_t nlocals
, nglobs
;
234 static int32_t def_seg
;
236 static struct RAA
*bsym
;
238 static struct SAA
*strs
;
239 static uint32_t strslen
;
243 static evalfunc evaluate
;
245 static struct Symbol
*fwds
;
247 static char elf_module
[FILENAME_MAX
];
249 static uint8_t elf_osabi
= 0; /* Default OSABI = 0 (System V or Linux) */
250 static uint8_t elf_abiver
= 0; /* Current ABI version */
252 extern struct ofmt of_elf64
;
256 #define SYM_GLOBAL 0x10
258 #define STV_DEFAULT 0
259 #define STV_INTERNAL 1
261 #define STV_PROTECTED 3
263 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
265 #define SEG_ALIGN 16 /* alignment of sections in file */
266 #define SEG_ALIGN_1 (SEG_ALIGN-1)
268 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
270 static struct ELF_SECTDATA
{
275 static int elf_nsect
;
276 static int64_t elf_foffs
;
278 static void elf_write(void);
279 static void elf_sect_write(struct Section
*, const uint8_t *,
281 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
283 static void elf_write_sections(void);
284 static struct SAA
*elf_build_symtab(int32_t *, int32_t *);
285 static struct SAA
*elf_build_reltab(uint64_t *, struct Reloc
*);
286 static void add_sectname(char *, char *);
288 /* this stuff is needed for the stabs debugging format */
289 #define N_SO 0x64 /* ID for main source file */
290 #define N_SOL 0x84 /* ID for sub-source file */
294 #define TY_STABSSYMLIN 0x40 /* internal call to debug_out */
310 int section
; /* index into sects[] */
311 int segto
; /* internal section number */
312 char *name
; /* shallow-copied pointer of section name */
316 struct symlininfo info
;
319 struct linelist
*next
;
320 struct linelist
*last
;
329 struct sectlist
*next
;
330 struct sectlist
*last
;
333 /* common debug variables */
334 static int currentline
= 1;
336 /* stabs debug variables */
337 static struct linelist
*stabslines
= 0;
338 static int stabs_immcall
= 0;
339 static int numlinestabs
= 0;
340 static char *stabs_filename
= 0;
341 static int symtabsection
;
342 static uint8_t *stabbuf
= 0, *stabstrbuf
= 0, *stabrelbuf
= 0;
343 static int stablen
, stabstrlen
, stabrellen
;
345 /* dwarf debug variables */
346 static struct linelist
*dwarf_flist
= 0, *dwarf_clist
= 0, *dwarf_elist
= 0;
347 static struct sectlist
*dwarf_fsect
= 0, *dwarf_csect
= 0, *dwarf_esect
= 0;
348 static int dwarf_immcall
= 0, dwarf_numfiles
= 0, dwarf_nsections
;
349 static uint8_t *arangesbuf
= 0, *arangesrelbuf
= 0, *pubnamesbuf
= 0, *infobuf
= 0, *inforelbuf
= 0,
350 *abbrevbuf
= 0, *linebuf
= 0, *linerelbuf
= 0, *framebuf
= 0, *locbuf
= 0;
351 static int8_t line_base
= -5, line_range
= 14, opcode_base
= 13;
352 static int arangeslen
, arangesrellen
, pubnameslen
, infolen
, inforellen
,
353 abbrevlen
, linelen
, linerellen
, framelen
, loclen
;
354 static char workbuf
[1024];
357 static struct dfmt df_dwarf
;
358 static struct dfmt df_stabs
;
359 static struct Symbol
*lastsym
;
361 void stabs64_init(struct ofmt
*, void *, FILE *, efunc
);
362 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t);
363 void stabs64_deflabel(char *, int32_t, int64_t, int, char *);
364 void stabs64_directive(const char *, const char *);
365 void stabs64_typevalue(int32_t);
366 void stabs64_output(int, void *);
367 void stabs64_generate(void);
368 void stabs64_cleanup(void);
370 /* dwarf debugging routines */
371 void dwarf64_init(struct ofmt
*, void *, FILE *, efunc
);
372 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t);
373 void dwarf64_deflabel(char *, int32_t, int64_t, int, char *);
374 void dwarf64_directive(const char *, const char *);
375 void dwarf64_typevalue(int32_t);
376 void dwarf64_output(int, void *);
377 void dwarf64_generate(void);
378 void dwarf64_cleanup(void);
379 void dwarf64_findfile(const char *);
380 void dwarf64_findsect(const int);
381 void saa_wleb128u(struct SAA
*, int);
382 void saa_wleb128s(struct SAA
*, int);
385 * Special section numbers which are used to define ELF special
386 * symbols, which can be used with WRT to provide PIC relocation
389 static int32_t elf_gotpc_sect
, elf_gotoff_sect
;
390 static int32_t elf_got_sect
, elf_plt_sect
;
391 static int32_t elf_sym_sect
;
393 static void elf_init(FILE * fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
399 (void)ldef
; /* placate optimisers */
401 nsects
= sectlen
= 0;
402 syms
= saa_init((int32_t)sizeof(struct Symbol
));
403 nlocals
= nglobs
= 0;
406 saa_wbytes(strs
, "\0", 1L);
407 saa_wbytes(strs
, elf_module
, (int32_t)(strlen(elf_module
) + 1));
408 strslen
= 2 + strlen(elf_module
);
410 shstrtablen
= shstrtabsize
= 0;;
411 add_sectname("", "");
415 elf_gotpc_sect
= seg_alloc();
416 ldef("..gotpc", elf_gotpc_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
418 elf_gotoff_sect
= seg_alloc();
419 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
421 elf_got_sect
= seg_alloc();
422 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
424 elf_plt_sect
= seg_alloc();
425 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
427 elf_sym_sect
= seg_alloc();
428 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, false, false, &of_elf64
,
431 def_seg
= seg_alloc();
435 static void elf_cleanup(int debuginfo
)
444 for (i
= 0; i
< nsects
; i
++) {
445 if (sects
[i
]->type
!= SHT_NOBITS
)
446 saa_free(sects
[i
]->data
);
448 saa_free(sects
[i
]->rel
);
449 while (sects
[i
]->head
) {
451 sects
[i
]->head
= sects
[i
]->head
->next
;
459 if (of_elf64
.current_dfmt
) {
460 of_elf64
.current_dfmt
->cleanup();
463 /* add entry to the elf .shstrtab section */
464 static void add_sectname(char *firsthalf
, char *secondhalf
)
466 int len
= strlen(firsthalf
) + strlen(secondhalf
);
467 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
468 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
469 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
470 strcat(shstrtab
+ shstrtablen
, secondhalf
);
471 shstrtablen
+= len
+ 1;
474 static int elf_make_section(char *name
, int type
, int flags
, int align
)
478 s
= nasm_malloc(sizeof(*s
));
480 if (type
!= SHT_NOBITS
)
481 s
->data
= saa_init(1L);
484 s
->len
= s
->size
= 0;
486 if (!strcmp(name
, ".text"))
489 s
->index
= seg_alloc();
490 add_sectname("", name
);
491 s
->name
= nasm_malloc(1 + strlen(name
));
492 strcpy(s
->name
, name
);
498 if (nsects
>= sectlen
)
500 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
506 static int32_t elf_section_names(char *name
, int pass
, int *bits
)
509 unsigned flags_and
, flags_or
;
510 uint64_t type
, align
;
514 * Default is 64 bits.
522 while (*p
&& !isspace(*p
))
526 flags_and
= flags_or
= type
= align
= 0;
528 while (*p
&& isspace(*p
))
532 while (*p
&& !isspace(*p
))
536 while (*p
&& isspace(*p
))
539 if (!nasm_strnicmp(q
, "align=", 6)) {
543 if ((align
- 1) & align
) { /* means it's not a power of two */
544 error(ERR_NONFATAL
, "section alignment %d is not"
545 " a power of two", align
);
548 } else if (!nasm_stricmp(q
, "alloc")) {
549 flags_and
|= SHF_ALLOC
;
550 flags_or
|= SHF_ALLOC
;
551 } else if (!nasm_stricmp(q
, "noalloc")) {
552 flags_and
|= SHF_ALLOC
;
553 flags_or
&= ~SHF_ALLOC
;
554 } else if (!nasm_stricmp(q
, "exec")) {
555 flags_and
|= SHF_EXECINSTR
;
556 flags_or
|= SHF_EXECINSTR
;
557 } else if (!nasm_stricmp(q
, "noexec")) {
558 flags_and
|= SHF_EXECINSTR
;
559 flags_or
&= ~SHF_EXECINSTR
;
560 } else if (!nasm_stricmp(q
, "write")) {
561 flags_and
|= SHF_WRITE
;
562 flags_or
|= SHF_WRITE
;
563 } else if (!nasm_stricmp(q
, "nowrite")) {
564 flags_and
|= SHF_WRITE
;
565 flags_or
&= ~SHF_WRITE
;
566 } else if (!nasm_stricmp(q
, "progbits")) {
568 } else if (!nasm_stricmp(q
, "nobits")) {
573 if (!strcmp(name
, ".comment") ||
574 !strcmp(name
, ".shstrtab") ||
575 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
576 error(ERR_NONFATAL
, "attempt to redefine reserved section"
581 for (i
= 0; i
< nsects
; i
++)
582 if (!strcmp(name
, sects
[i
]->name
))
585 if (!strcmp(name
, ".text"))
586 i
= elf_make_section(name
, SHT_PROGBITS
,
587 SHF_ALLOC
| SHF_EXECINSTR
, 16);
588 else if (!strcmp(name
, ".rodata"))
589 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
590 else if (!strcmp(name
, ".data"))
591 i
= elf_make_section(name
, SHT_PROGBITS
,
592 SHF_ALLOC
| SHF_WRITE
, 4);
593 else if (!strcmp(name
, ".bss"))
594 i
= elf_make_section(name
, SHT_NOBITS
,
595 SHF_ALLOC
| SHF_WRITE
, 4);
597 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
599 sects
[i
]->type
= type
;
601 sects
[i
]->align
= align
;
602 sects
[i
]->flags
&= ~flags_and
;
603 sects
[i
]->flags
|= flags_or
;
604 } else if (pass
== 1) {
605 if ((type
&& sects
[i
]->type
!= type
)
606 || (align
&& sects
[i
]->align
!= align
)
607 || (flags_and
&& ((sects
[i
]->flags
& flags_and
) != flags_or
)))
608 error(ERR_WARNING
, "incompatible section attributes ignored on"
609 " redeclaration of section `%s'", name
);
612 return sects
[i
]->index
;
615 static void elf_deflabel(char *name
, int32_t segment
, int64_t offset
,
616 int is_global
, char *special
)
620 bool special_used
= false;
622 #if defined(DEBUG) && DEBUG>2
624 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
625 name
, segment
, offset
, is_global
, special
);
627 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
629 * This is a NASM special symbol. We never allow it into
630 * the ELF symbol table, even if it's a valid one. If it
631 * _isn't_ a valid one, we should barf immediately.
633 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
634 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
635 strcmp(name
, "..sym"))
636 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
640 if (is_global
== 3) {
643 * Fix up a forward-reference symbol size from the first
646 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
647 if (!strcmp((*s
)->name
, name
)) {
648 struct tokenval tokval
;
652 while (*p
&& !isspace(*p
))
654 while (*p
&& isspace(*p
))
658 tokval
.t_type
= TOKEN_INVALID
;
659 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
662 error(ERR_NONFATAL
, "cannot use relocatable"
663 " expression as symbol size");
665 (*s
)->size
= reloc_value(e
);
669 * Remove it from the list of unresolved sizes.
671 nasm_free((*s
)->name
);
675 return; /* it wasn't an important one */
678 saa_wbytes(strs
, name
, (int32_t)(1 + strlen(name
)));
679 strslen
+= 1 + strlen(name
);
681 lastsym
= sym
= saa_wstruct(syms
);
684 sym
->type
= is_global
? SYM_GLOBAL
: 0;
685 sym
->other
= STV_DEFAULT
;
687 if (segment
== NO_SEG
)
688 sym
->section
= SHN_ABS
;
691 sym
->section
= SHN_UNDEF
;
692 if (nsects
== 0 && segment
== def_seg
) {
694 if (segment
!= elf_section_names(".text", 2, &tempint
))
696 "strange segment conditions in ELF driver");
697 sym
->section
= nsects
;
699 for (i
= 0; i
< nsects
; i
++)
700 if (segment
== sects
[i
]->index
) {
701 sym
->section
= i
+ 1;
707 if (is_global
== 2) {
710 sym
->section
= SHN_COMMON
;
712 * We have a common variable. Check the special text to see
713 * if it's a valid number and power of two; if so, store it
714 * as the alignment for the common variable.
718 sym
->value
= readnum(special
, &err
);
720 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
721 " valid number", special
);
722 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
723 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
724 " power of two", special
);
728 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
730 if (sym
->type
== SYM_GLOBAL
) {
732 * If sym->section == SHN_ABS, then the first line of the
733 * else section would cause a core dump, because its a reference
734 * beyond the end of the section array.
735 * This behaviour is exhibited by this code:
738 * To avoid such a crash, such requests are silently discarded.
739 * This may not be the best solution.
741 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
742 bsym
= raa_write(bsym
, segment
, nglobs
);
743 } else if (sym
->section
!= SHN_ABS
) {
745 * This is a global symbol; so we must add it to the linked
746 * list of global symbols in its section. We'll push it on
747 * the beginning of the list, because it doesn't matter
748 * much which end we put it on and it's easier like this.
750 * In addition, we check the special text for symbol
751 * type and size information.
753 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
754 sects
[sym
->section
- 1]->gsyms
= sym
;
757 int n
= strcspn(special
, " \t");
759 if (!nasm_strnicmp(special
, "function", n
))
760 sym
->type
|= STT_FUNC
;
761 else if (!nasm_strnicmp(special
, "data", n
) ||
762 !nasm_strnicmp(special
, "object", n
))
763 sym
->type
|= STT_OBJECT
;
764 else if (!nasm_strnicmp(special
, "notype", n
))
765 sym
->type
|= STT_NOTYPE
;
767 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
771 while (isspace(*special
))
774 n
= strcspn(special
, " \t");
775 if (!nasm_strnicmp(special
, "default", n
))
776 sym
->other
= STV_DEFAULT
;
777 else if (!nasm_strnicmp(special
, "internal", n
))
778 sym
->other
= STV_INTERNAL
;
779 else if (!nasm_strnicmp(special
, "hidden", n
))
780 sym
->other
= STV_HIDDEN
;
781 else if (!nasm_strnicmp(special
, "protected", n
))
782 sym
->other
= STV_PROTECTED
;
789 struct tokenval tokval
;
792 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
794 while (special
[n
] && isspace(special
[n
]))
797 * We have a size expression; attempt to
801 stdscan_bufptr
= special
+ n
;
802 tokval
.t_type
= TOKEN_INVALID
;
803 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
808 sym
->name
= nasm_strdup(name
);
811 error(ERR_NONFATAL
, "cannot use relocatable"
812 " expression as symbol size");
814 sym
->size
= reloc_value(e
);
816 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
821 sym
->globnum
= nglobs
;
826 if (special
&& !special_used
)
827 error(ERR_NONFATAL
, "no special symbol features supported here");
830 static void elf_add_reloc(struct Section
*sect
, int32_t segment
, int type
)
833 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
834 sect
->tail
= &r
->next
;
837 r
->address
= sect
->len
;
838 if (segment
== NO_SEG
)
843 for (i
= 0; i
< nsects
; i
++)
844 if (segment
== sects
[i
]->index
)
847 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
855 * This routine deals with ..got and ..sym relocations: the more
856 * complicated kinds. In shared-library writing, some relocations
857 * with respect to global symbols must refer to the precise symbol
858 * rather than referring to an offset from the base of the section
859 * _containing_ the symbol. Such relocations call to this routine,
860 * which searches the symbol list for the symbol in question.
862 * R_386_GOT32 references require the _exact_ symbol address to be
863 * used; R_386_32 references can be at an offset from the symbol.
864 * The boolean argument `exact' tells us this.
866 * Return value is the adjusted value of `addr', having become an
867 * offset from the symbol rather than the section. Should always be
868 * zero when returning from an exact call.
870 * Limitation: if you define two symbols at the same place,
871 * confusion will occur.
873 * Inefficiency: we search, currently, using a linked list which
874 * isn't even necessarily sorted.
876 static int32_t elf_add_gsym_reloc(struct Section
*sect
,
877 int32_t segment
, int64_t offset
,
878 int type
, bool exact
)
882 struct Symbol
*sym
, *sm
;
886 * First look up the segment/offset pair and find a global
887 * symbol corresponding to it. If it's not one of our segments,
888 * then it must be an external symbol, in which case we're fine
889 * doing a normal elf_add_reloc after first sanity-checking
890 * that the offset from the symbol is zero.
893 for (i
= 0; i
< nsects
; i
++)
894 if (segment
== sects
[i
]->index
) {
899 if (exact
&& offset
!= 0)
900 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
901 " for this reference");
903 elf_add_reloc(sect
, segment
, type
);
909 * Find a symbol pointing _exactly_ at this one.
911 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
912 if (sym
->value
== offset
)
916 * Find the nearest symbol below this one.
919 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
920 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
924 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
925 " for this reference");
929 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
930 sect
->tail
= &r
->next
;
933 r
->address
= sect
->len
;
934 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
939 return offset
- sym
->value
;
942 static void elf_out(int32_t segto
, const void *data
,
943 enum out_type type
, uint64_t size
,
944 int32_t segment
, int32_t wrt
)
948 uint8_t mydata
[16], *p
;
950 static struct symlininfo sinfo
;
952 #if defined(DEBUG) && DEBUG>2
953 if (data
) fprintf(stderr
,
954 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x data: %"PRIx64
"\n",
955 currentline
, type
, segment
, segto
, size
, *(int64_t *)data
);
957 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
958 currentline
, type
, segment
, segto
, size
);
962 * handle absolute-assembly (structure definitions)
964 if (segto
== NO_SEG
) {
965 if (type
!= OUT_RESERVE
)
966 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
972 for (i
= 0; i
< nsects
; i
++)
973 if (segto
== sects
[i
]->index
) {
978 int tempint
; /* ignored */
979 if (segto
!= elf_section_names(".text", 2, &tempint
))
980 error(ERR_PANIC
, "strange segment conditions in ELF driver");
982 s
= sects
[nsects
- 1];
987 /* invoke current debug_output routine */
988 if (of_elf64
.current_dfmt
) {
989 sinfo
.offset
= s
->len
;
992 sinfo
.name
= s
->name
;
993 of_elf64
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
995 /* end of debugging stuff */
997 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
998 error(ERR_WARNING
, "attempt to initialize memory in"
999 " BSS section `%s': ignored", s
->name
);
1000 if (type
== OUT_REL2ADR
)
1002 else if (type
== OUT_REL4ADR
)
1008 if (type
== OUT_RESERVE
) {
1009 if (s
->type
== SHT_PROGBITS
) {
1010 error(ERR_WARNING
, "uninitialized space declared in"
1011 " non-BSS section `%s': zeroing", s
->name
);
1012 elf_sect_write(s
, NULL
, size
);
1015 } else if (type
== OUT_RAWDATA
) {
1016 if (segment
!= NO_SEG
)
1017 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
1018 elf_sect_write(s
, data
, size
);
1019 } else if (type
== OUT_ADDRESS
) {
1021 addr
= *(int64_t *)data
;
1022 if (segment
!= NO_SEG
) {
1024 error(ERR_NONFATAL
, "ELF format does not support"
1025 " segment base references");
1027 if (wrt
== NO_SEG
) {
1028 switch ((int)size
) {
1030 elf_add_reloc(s
, segment
, R_X86_64_16
);
1033 elf_add_reloc(s
, segment
, R_X86_64_32
);
1036 elf_add_reloc(s
, segment
, R_X86_64_64
);
1039 error(ERR_PANIC
, "internal error elf64-hpa-871");
1042 } else if (wrt
== elf_gotpc_sect
+ 1) {
1044 * The user will supply GOT relative to $$. ELF
1045 * will let us have GOT relative to $. So we
1046 * need to fix up the data item by $-$$.
1049 elf_add_reloc(s
, segment
, R_X86_64_GOTPCREL
);
1050 } else if (wrt
== elf_gotoff_sect
+ 1) {
1051 elf_add_reloc(s
, segment
, R_X86_64_GOTTPOFF
);
1052 } else if (wrt
== elf_got_sect
+ 1) {
1053 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
1054 R_X86_64_GOT32
, true);
1055 } else if (wrt
== elf_sym_sect
+ 1) {
1056 switch ((int)size
) {
1059 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
1060 R_X86_64_16
, false);
1063 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
1064 R_X86_64_32
, false);
1067 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
1068 R_X86_64_64
, false);
1071 error(ERR_PANIC
, "internal error elf64-hpa-903");
1074 } else if (wrt
== elf_plt_sect
+ 1) {
1075 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
1076 "relative PLT references");
1078 error(ERR_NONFATAL
, "ELF format does not support this"
1080 wrt
= NO_SEG
; /* we can at least _try_ to continue */
1086 WRITESHORT(p
, addr
);
1088 if (size
!= 8 && size
!= 4 && segment
!= NO_SEG
) {
1090 "Unsupported non-64-bit ELF relocation");
1092 if (size
== 4) WRITELONG(p
, addr
);
1093 else WRITEDLONG(p
, (int64_t)addr
);
1095 elf_sect_write(s
, mydata
, size
);
1096 } else if (type
== OUT_REL2ADR
) {
1097 if (segment
== segto
)
1098 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
1099 if (segment
!= NO_SEG
&& segment
% 2) {
1100 error(ERR_NONFATAL
, "ELF format does not support"
1101 " segment base references");
1103 if (wrt
== NO_SEG
) {
1104 elf_add_reloc(s
, segment
, R_X86_64_PC16
);
1107 "Unsupported non-32-bit ELF relocation [2]");
1111 WRITESHORT(p
, *(int64_t *)data
- size
);
1112 elf_sect_write(s
, mydata
, 2L);
1113 } else if (type
== OUT_REL4ADR
) {
1114 if (segment
== segto
)
1115 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
1116 if (segment
!= NO_SEG
&& segment
% 2) {
1117 error(ERR_NONFATAL
, "ELF format does not support"
1118 " segment base references");
1120 if (wrt
== NO_SEG
) {
1121 elf_add_reloc(s
, segment
, R_X86_64_PC32
);
1122 } else if (wrt
== elf_plt_sect
+ 1) {
1123 elf_add_reloc(s
, segment
, R_X86_64_PLT32
);
1124 } else if (wrt
== elf_gotpc_sect
+ 1 ||
1125 wrt
== elf_gotoff_sect
+ 1 ||
1126 wrt
== elf_got_sect
+ 1) {
1127 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
1128 "relative GOT references");
1130 error(ERR_NONFATAL
, "ELF format does not support this"
1132 wrt
= NO_SEG
; /* we can at least _try_ to continue */
1136 WRITELONG(p
, *(int64_t *)data
- size
);
1137 elf_sect_write(s
, mydata
, 4L);
1141 static void elf_write(void)
1143 int nsections
, align
;
1151 int32_t symtablen
, symtablocal
;
1154 * Work out how many sections we will have. We have SHN_UNDEF,
1155 * then the flexible user sections, then the four fixed
1156 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1157 * then optionally relocation sections for the user sections.
1159 if (of_elf64
.current_dfmt
== &df_stabs
)
1161 else if (of_elf64
.current_dfmt
== &df_dwarf
)
1164 nsections
= 5; /* SHN_UNDEF and the fixed ones */
1166 add_sectname("", ".comment");
1167 add_sectname("", ".shstrtab");
1168 add_sectname("", ".symtab");
1169 add_sectname("", ".strtab");
1170 for (i
= 0; i
< nsects
; i
++) {
1171 nsections
++; /* for the section itself */
1172 if (sects
[i
]->head
) {
1173 nsections
++; /* for its relocations */
1174 add_sectname(".rela", sects
[i
]->name
);
1178 if (of_elf64
.current_dfmt
== &df_stabs
) {
1179 /* in case the debug information is wanted, just add these three sections... */
1180 add_sectname("", ".stab");
1181 add_sectname("", ".stabstr");
1182 add_sectname(".rel", ".stab");
1185 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1186 /* the dwarf debug standard specifies the following ten sections,
1187 not all of which are currently implemented,
1188 although all of them are defined. */
1189 #define debug_aranges nsections-10
1190 #define debug_info nsections-7
1191 #define debug_line nsections-4
1192 add_sectname("", ".debug_aranges");
1193 add_sectname(".rela", ".debug_aranges");
1194 add_sectname("", ".debug_pubnames");
1195 add_sectname("", ".debug_info");
1196 add_sectname(".rela", ".debug_info");
1197 add_sectname("", ".debug_abbrev");
1198 add_sectname("", ".debug_line");
1199 add_sectname(".rela", ".debug_line");
1200 add_sectname("", ".debug_frame");
1201 add_sectname("", ".debug_loc");
1209 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
1212 * Output the ELF header.
1214 fwrite("\177ELF\2\1\1", 7, 1, elffp
);
1215 fputc(elf_osabi
, elffp
);
1216 fputc(elf_abiver
, elffp
);
1217 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp
);
1218 fwriteint16_t(ET_REL
, elffp
); /* relocatable file */
1219 fwriteint16_t(EM_X86_64
, elffp
); /* processor ID */
1220 fwriteint32_t(1L, elffp
); /* EV_CURRENT file format version */
1221 fwriteint64_t(0L, elffp
); /* no entry point */
1222 fwriteint64_t(0L, elffp
); /* no program header table */
1223 fwriteint64_t(0x40L
, elffp
); /* section headers straight after
1224 * ELF header plus alignment */
1225 fwriteint32_t(0L, elffp
); /* 386 defines no special flags */
1226 fwriteint16_t(0x40, elffp
); /* size of ELF header */
1227 fwriteint16_t(0, elffp
); /* no program header table, again */
1228 fwriteint16_t(0, elffp
); /* still no program header table */
1229 fwriteint16_t(sizeof(Elf64_Shdr
), elffp
); /* size of section header */
1230 fwriteint16_t(nsections
, elffp
); /* number of sections */
1231 fwriteint16_t(nsects
+ 2, elffp
); /* string table section index for
1232 * section header table */
1235 * Build the symbol table and relocation tables.
1237 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
1238 for (i
= 0; i
< nsects
; i
++)
1240 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
1244 * Now output the section header table.
1247 elf_foffs
= 0x40 + sizeof(Elf64_Shdr
) * nsections
;
1248 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1251 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * nsections
);
1252 elf_section_header(0, 0, 0, NULL
, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1253 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1255 for (i
= 0; i
< nsects
; i
++) {
1256 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1257 (sects
[i
]->type
== SHT_PROGBITS
?
1258 sects
[i
]->data
: NULL
), true,
1259 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1261 scount
++; /* ditto */
1263 elf_section_header(p
- shstrtab
, 1, 0, comment
, false, (int32_t)commlen
, 0, 0, 1, 0); /* .comment */
1264 scount
++; /* ditto */
1266 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, false, (int32_t)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1267 scount
++; /* ditto */
1269 elf_section_header(p
- shstrtab
, 2, 0, symtab
, true, symtablen
, nsects
+ 4, symtablocal
, 4, 24); /* .symtab */
1270 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1272 elf_section_header(p
- shstrtab
, 3, 0, strs
, true, strslen
, 0, 0, 1, 0); /* .strtab */
1273 for (i
= 0; i
< nsects
; i
++)
1274 if (sects
[i
]->head
) {
1276 elf_section_header(p
- shstrtab
,SHT_RELA
, 0, sects
[i
]->rel
, true,
1277 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 24);
1279 if (of_elf64
.current_dfmt
== &df_stabs
) {
1280 /* for debugging information, create the last three sections
1281 which are the .stab , .stabstr and .rel.stab sections respectively */
1283 /* this function call creates the stab sections in memory */
1286 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1288 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, false, stablen
,
1289 nsections
- 2, 0, 4, 12);
1292 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, false,
1293 stabstrlen
, 0, 0, 4, 0);
1296 /* link -> symtable info -> section to refer to */
1297 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, false,
1298 stabrellen
, symtabsection
, nsections
- 3, 4,
1302 else if (of_elf64
.current_dfmt
== &df_dwarf
) {
1303 /* for dwarf debugging information, create the ten dwarf sections */
1305 /* this function call creates the dwarf sections in memory */
1309 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, arangesbuf
, false,
1310 arangeslen
, 0, 0, 1, 0);
1312 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, arangesrelbuf
, false,
1313 arangesrellen
, symtabsection
, debug_aranges
, 1, 24);
1315 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, pubnamesbuf
, false,
1316 pubnameslen
, 0, 0, 1, 0);
1318 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, infobuf
, false,
1319 infolen
, 0, 0, 1, 0);
1321 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, inforelbuf
, false,
1322 inforellen
, symtabsection
, debug_info
, 1, 24);
1324 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, abbrevbuf
, false,
1325 abbrevlen
, 0, 0, 1, 0);
1327 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, linebuf
, false,
1328 linelen
, 0, 0, 1, 0);
1330 elf_section_header(p
- shstrtab
, SHT_RELA
, 0, linerelbuf
, false,
1331 linerellen
, symtabsection
, debug_line
, 1, 24);
1333 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, framebuf
, false,
1334 framelen
, 0, 0, 8, 0);
1336 elf_section_header(p
- shstrtab
, SHT_PROGBITS
, 0, locbuf
, false,
1337 loclen
, 0, 0, 1, 0);
1340 fwrite(align_str
, align
, 1, elffp
);
1343 * Now output the sections.
1345 elf_write_sections();
1347 nasm_free(elf_sects
);
1351 static struct SAA
*elf_build_symtab(int32_t *len
, int32_t *local
)
1353 struct SAA
*s
= saa_init(1L);
1355 uint8_t entry
[24], *p
;
1361 * First, an all-zeros entry, required by the ELF spec.
1363 saa_wbytes(s
, NULL
, 24L); /* null symbol table entry */
1368 * Next, an entry for the file name.
1371 WRITELONG(p
, 1); /* we know it's 1st entry in strtab */
1372 WRITESHORT(p
, STT_FILE
); /* type FILE */
1373 WRITESHORT(p
, SHN_ABS
);
1374 WRITEDLONG(p
, (uint64_t) 0); /* no value */
1375 WRITEDLONG(p
, (uint64_t) 0); /* no size either */
1376 saa_wbytes(s
, entry
, 24L);
1381 * Now some standard symbols defining the segments, for relocation
1384 for (i
= 1; i
<= nsects
; i
++) {
1386 WRITELONG(p
, 0); /* no symbol name */
1387 WRITESHORT(p
, STT_SECTION
); /* type, binding, and visibility */
1388 WRITESHORT(p
, i
); /* section id */
1389 WRITEDLONG(p
, (uint64_t) 0); /* offset zero */
1390 WRITEDLONG(p
, (uint64_t) 0); /* size zero */
1391 saa_wbytes(s
, entry
, 24L);
1397 * Now the other local symbols.
1400 while ((sym
= saa_rstruct(syms
))) {
1401 if (sym
->type
& SYM_GLOBAL
)
1404 WRITELONG(p
, sym
->strpos
);
1405 WRITECHAR(p
, sym
->type
); /* type and binding */
1406 WRITECHAR(p
, sym
->other
); /* visibility */
1407 WRITESHORT(p
, sym
->section
);
1408 WRITEDLONG(p
, (int64_t)sym
->value
);
1409 WRITEDLONG(p
, (int64_t)sym
->size
);
1410 saa_wbytes(s
, entry
, 24L);
1416 * Now the global symbols.
1419 while ((sym
= saa_rstruct(syms
))) {
1420 if (!(sym
->type
& SYM_GLOBAL
))
1423 WRITELONG(p
, sym
->strpos
);
1424 WRITECHAR(p
, sym
->type
); /* type and binding */
1425 WRITECHAR(p
, sym
->other
); /* visibility */
1426 WRITESHORT(p
, sym
->section
);
1427 WRITEDLONG(p
, (int64_t)sym
->value
);
1428 WRITEDLONG(p
, (int64_t)sym
->size
);
1429 saa_wbytes(s
, entry
, 24L);
1436 static struct SAA
*elf_build_reltab(uint64_t *len
, struct Reloc
*r
)
1439 uint8_t *p
, entry
[24];
1448 int64_t sym
= r
->symbol
;
1450 if (sym
>= GLOBAL_TEMP_BASE
)
1451 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 2) + nlocals
;
1454 WRITEDLONG(p
, r
->address
);
1455 WRITEDLONG(p
, (sym
<< 32) + r
->type
);
1456 WRITEDLONG(p
, (uint64_t) 0);
1457 saa_wbytes(s
, entry
, 24L);
1466 static void elf_section_header(int name
, int type
, uint64_t flags
,
1467 void *data
, bool is_saa
, uint64_t datalen
,
1468 int link
, int info
, int align
, int eltsize
)
1470 elf_sects
[elf_nsect
].data
= data
;
1471 elf_sects
[elf_nsect
].len
= datalen
;
1472 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1475 fwriteint32_t((int32_t)name
, elffp
);
1476 fwriteint32_t((int32_t)type
, elffp
);
1477 fwriteint64_t((int64_t)flags
, elffp
);
1478 fwriteint64_t(0L, elffp
); /* no address, ever, in object files */
1479 fwriteint64_t(type
== 0 ? 0L : elf_foffs
, elffp
);
1480 fwriteint64_t(datalen
, elffp
);
1482 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1483 fwriteint32_t((int32_t)link
, elffp
);
1484 fwriteint32_t((int32_t)info
, elffp
);
1485 fwriteint64_t((int64_t)align
, elffp
);
1486 fwriteint64_t((int64_t)eltsize
, elffp
);
1489 static void elf_write_sections(void)
1492 for (i
= 0; i
< elf_nsect
; i
++)
1493 if (elf_sects
[i
].data
) {
1494 int32_t len
= elf_sects
[i
].len
;
1495 int32_t reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1496 int32_t align
= reallen
- len
;
1497 if (elf_sects
[i
].is_saa
)
1498 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1500 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1501 fwrite(align_str
, align
, 1, elffp
);
1505 static void elf_sect_write(struct Section
*sect
,
1506 const uint8_t *data
, uint64_t len
)
1508 saa_wbytes(sect
->data
, data
, len
);
1512 static int32_t elf_segbase(int32_t segment
)
1517 static int elf_directive(char *directive
, char *value
, int pass
)
1523 if (!strcmp(directive
, "osabi")) {
1525 return 1; /* ignore in pass 2 */
1527 n
= readnum(value
, &err
);
1529 error(ERR_NONFATAL
, "`osabi' directive requires a parameter");
1532 if (n
< 0 || n
> 255) {
1533 error(ERR_NONFATAL
, "valid osabi numbers are 0 to 255");
1539 if ((p
= strchr(value
,',')) == NULL
)
1542 n
= readnum(p
+1, &err
);
1543 if (err
|| n
< 0 || n
> 255) {
1544 error(ERR_NONFATAL
, "invalid ABI version number (valid: 0 to 255)");
1555 static void elf_filename(char *inname
, char *outname
, efunc error
)
1557 strcpy(elf_module
, inname
);
1558 standard_extension(inname
, outname
, ".o", error
);
1561 static const char *elf_stdmac
[] = {
1562 "%define __SECT__ [section .text]",
1563 "%macro __NASM_CDecl__ 1",
1566 "%macro osabi 1+.nolist",
1571 static int elf_set_info(enum geninfo type
, char **val
)
1577 static struct dfmt df_dwarf
= {
1578 "ELF64 (X86_64) dwarf debug format for Linux",
1588 static struct dfmt df_stabs
= {
1589 "ELF64 (X86_64) stabs debug format for Linux",
1600 struct dfmt
*elf64_debugs_arr
[3] = { &df_stabs
, &df_dwarf
, NULL
};
1602 struct ofmt of_elf64
= {
1603 "ELF64 (x86_64) object files (e.g. Linux)",
1620 /* again, the stabs debugging stuff (code) */
1622 void stabs64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1630 void stabs64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1633 if (!stabs_filename
) {
1634 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1635 strcpy(stabs_filename
, filename
);
1637 if (strcmp(stabs_filename
, filename
)) {
1638 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1639 in fact, this leak comes in quite handy to maintain a list of files
1640 encountered so far in the symbol lines... */
1642 /* why not nasm_free(stabs_filename); we're done with the old one */
1644 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1645 strcpy(stabs_filename
, filename
);
1649 currentline
= linenumber
;
1652 void stabs64_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1662 void stabs64_directive(const char *directive
, const char *params
)
1668 void stabs64_typevalue(int32_t type
)
1670 int32_t stype
, ssize
;
1671 switch (TYM_TYPE(type
)) {
1710 stype
= STT_SECTION
;
1725 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
1726 lastsym
->size
= ssize
;
1727 lastsym
->type
= stype
;
1731 void stabs64_output(int type
, void *param
)
1733 struct symlininfo
*s
;
1734 struct linelist
*el
;
1735 if (type
== TY_STABSSYMLIN
) {
1736 if (stabs_immcall
) {
1737 s
= (struct symlininfo
*)param
;
1738 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
1739 return; /* line info is only collected for executable sections */
1741 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1742 el
->info
.offset
= s
->offset
;
1743 el
->info
.section
= s
->section
;
1744 el
->info
.name
= s
->name
;
1745 el
->line
= currentline
;
1746 el
->filename
= stabs_filename
;
1749 stabslines
->last
->next
= el
;
1750 stabslines
->last
= el
;
1753 stabslines
->last
= el
;
1760 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1762 WRITELONG(p,n_strx); \
1763 WRITECHAR(p,n_type); \
1764 WRITECHAR(p,n_other); \
1765 WRITESHORT(p,n_desc); \
1766 WRITELONG(p,n_value); \
1769 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1771 void stabs64_generate(void)
1773 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1774 uint8_t *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1778 struct linelist
*ptr
;
1782 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(int8_t *));
1783 for (i
= 0; i
< numlinestabs
; i
++)
1787 if (numfiles
== 0) {
1788 allfiles
[0] = ptr
->filename
;
1791 for (i
= 0; i
< numfiles
; i
++) {
1792 if (!strcmp(allfiles
[i
], ptr
->filename
))
1795 if (i
>= numfiles
) {
1796 allfiles
[i
] = ptr
->filename
;
1803 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1804 for (i
= 0; i
< numfiles
; i
++) {
1805 fileidx
[i
] = strsize
;
1806 strsize
+= strlen(allfiles
[i
]) + 1;
1809 for (i
= 0; i
< numfiles
; i
++) {
1810 if (!strcmp(allfiles
[i
], elf_module
)) {
1816 /* worst case size of the stab buffer would be:
1817 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1820 (uint8_t *)nasm_malloc((numlinestabs
* 2 + 3) *
1821 sizeof(struct stabentry
));
1823 ssbuf
= (uint8_t *)nasm_malloc(strsize
);
1825 rbuf
= (uint8_t *)nasm_malloc(numlinestabs
* 16 * (2 + 3));
1828 for (i
= 0; i
< numfiles
; i
++) {
1829 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1833 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1840 /* this is the first stab, its strx points to the filename of the
1841 the source-file, the n_desc field should be set to the number
1844 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1846 /* this is the stab for the main source file */
1847 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1849 /* relocation table entry */
1851 /* Since the symbol table has two entries before */
1852 /* the section symbols, the index in the info.section */
1853 /* member must be adjusted by adding 2 */
1855 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1856 WRITELONG(rptr
, R_X86_64_32
);
1857 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1860 currfile
= mainfileindex
;
1864 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1865 /* oops file has changed... */
1866 for (i
= 0; i
< numfiles
; i
++)
1867 if (!strcmp(allfiles
[i
], ptr
->filename
))
1870 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1874 /* relocation table entry */
1876 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1877 WRITELONG(rptr
, R_X86_64_32
);
1878 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1881 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1884 /* relocation table entry */
1886 WRITEDLONG(rptr
, (int64_t)(sptr
- sbuf
) - 4);
1887 WRITELONG(rptr
, R_X86_64_32
);
1888 WRITELONG(rptr
, ptr
->info
.section
+ 2);
1894 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1896 nasm_free(allfiles
);
1899 stablen
= (sptr
- sbuf
);
1900 stabrellen
= (rptr
- rbuf
);
1906 void stabs64_cleanup(void)
1908 struct linelist
*ptr
, *del
;
1920 nasm_free(stabrelbuf
);
1922 nasm_free(stabstrbuf
);
1924 /* dwarf routines */
1926 void dwarf64_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1934 void dwarf64_linenum(const char *filename
, int32_t linenumber
, int32_t segto
)
1937 dwarf64_findfile(filename
);
1939 currentline
= linenumber
;
1942 void dwarf64_deflabel(char *name
, int32_t segment
, int64_t offset
, int is_global
,
1952 void dwarf64_directive(const char *directive
, const char *params
)
1958 void dwarf64_typevalue(int32_t type
)
1960 int32_t stype
, ssize
;
1961 switch (TYM_TYPE(type
)) {
2000 stype
= STT_SECTION
;
2015 if (stype
== STT_OBJECT
&& lastsym
&& !lastsym
->type
) {
2016 lastsym
->size
= ssize
;
2017 lastsym
->type
= stype
;
2020 /* called from elf_out with type == TY_STABSSYMLIN */
2021 void dwarf64_output(int type
, void *param
)
2023 int ln
, aa
, inx
, maxln
, soc
;
2024 struct symlininfo
*s
;
2029 s
= (struct symlininfo
*)param
;
2030 /* line number info is only gathered for executable sections */
2031 if (!(sects
[s
->section
]->flags
& SHF_EXECINSTR
))
2033 /* Check if section index has changed */
2034 if (!(dwarf_csect
&& (dwarf_csect
->section
) == (s
->section
)))
2036 dwarf64_findsect(s
->section
);
2038 /* do nothing unless line or file has changed */
2041 ln
= currentline
- dwarf_csect
->line
;
2042 aa
= s
->offset
- dwarf_csect
->offset
;
2043 inx
= dwarf_clist
->line
;
2044 plinep
= dwarf_csect
->psaa
;
2045 /* check for file change */
2046 if (!(inx
== dwarf_csect
->file
))
2048 WSAACHAR(plinep
,workbuf
,DW_LNS_set_file
);
2049 WSAACHAR(plinep
,workbuf
,inx
);
2050 dwarf_csect
->file
= inx
;
2052 /* check for line change */
2055 /* test if in range of special op code */
2056 maxln
= line_base
+ line_range
;
2057 soc
= (ln
- line_base
) + (line_range
* aa
) + opcode_base
;
2058 if (ln
>= line_base
&& ln
< maxln
&& soc
< 256)
2060 WSAACHAR(plinep
,workbuf
,soc
);
2066 WSAACHAR(plinep
,workbuf
,DW_LNS_advance_line
);
2067 saa_wleb128s(plinep
,ln
);
2071 WSAACHAR(plinep
,workbuf
,DW_LNS_advance_pc
);
2072 saa_wleb128u(plinep
,aa
);
2075 dwarf_csect
->line
= currentline
;
2076 dwarf_csect
->offset
= s
->offset
;
2078 /* show change handled */
2084 void dwarf64_generate(void)
2088 struct linelist
*ftentry
;
2089 struct SAA
*paranges
, *ppubnames
, *pinfo
, *pabbrev
, *plines
, *plinep
;
2090 struct SAA
*parangesrel
, *plinesrel
;
2091 struct sectlist
*psect
;
2092 size_t saalen
, linepoff
, totlen
, highaddr
;
2094 /* write epilogues for each line program range */
2095 /* and build aranges section */
2096 paranges
= saa_init(1L);
2097 parangesrel
= saa_init(1L);
2098 WSAASHORT(paranges
,workbuf
,3); /* dwarf version */
2099 WSAALONG(paranges
,workbuf
,0); /* offset into info */
2100 WSAACHAR(paranges
,workbuf
,8); /* pointer size */
2101 WSAACHAR(paranges
,workbuf
,0); /* not segmented */
2102 WSAALONG(paranges
,workbuf
,0); /* padding */
2103 /* iterate though sectlist entries */
2104 psect
= dwarf_fsect
;
2107 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2109 plinep
= psect
->psaa
;
2110 /* Line Number Program Epilogue */
2111 WSAACHAR(plinep
,workbuf
,2); /* std op 2 */
2112 WSAACHAR(plinep
,workbuf
,(sects
[psect
->section
]->len
)-psect
->offset
);
2113 WSAACHAR(plinep
,workbuf
,DW_LNS_extended_op
);
2114 WSAACHAR(plinep
,workbuf
,1); /* operand length */
2115 WSAACHAR(plinep
,workbuf
,DW_LNE_end_sequence
);
2116 totlen
+= plinep
->datalen
;
2117 /* range table relocation entry */
2118 WSAADLONG(parangesrel
,workbuf
, paranges
->datalen
+ 4);
2119 WSAADLONG(parangesrel
,workbuf
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2120 WSAADLONG(parangesrel
,workbuf
, (uint64_t) 0);
2121 /* range table entry */
2122 WSAADLONG(paranges
,workbuf
,0x0000); /* range start */
2123 WSAADLONG(paranges
,workbuf
,sects
[psect
->section
]->len
); /* range length */
2124 highaddr
+= sects
[psect
->section
]->len
;
2125 /* done with this entry */
2126 psect
= psect
->next
;
2128 WSAADLONG(paranges
,workbuf
,0); /* null address */
2129 WSAADLONG(paranges
,workbuf
,0); /* null length */
2130 saalen
= paranges
->datalen
;
2131 arangeslen
= saalen
+ 4;
2132 arangesbuf
= pbuf
= nasm_malloc(arangeslen
);
2133 WRITELONG(pbuf
,saalen
); /* initial length */
2134 memcpy(pbuf
, saa_rbytes(paranges
, &saalen
), saalen
);
2137 /* build rela.aranges section */
2138 arangesrellen
= saalen
= parangesrel
->datalen
;
2139 arangesrelbuf
= pbuf
= nasm_malloc(arangesrellen
);
2140 memcpy(pbuf
, saa_rbytes(parangesrel
, &saalen
), saalen
);
2141 saa_free(parangesrel
);
2143 /* build pubnames section */
2144 ppubnames
= saa_init(1L);
2145 WSAASHORT(ppubnames
,workbuf
,3); /* dwarf version */
2146 WSAALONG(ppubnames
,workbuf
,0); /* offset into info */
2147 WSAALONG(ppubnames
,workbuf
,0); /* space used in info */
2148 WSAALONG(ppubnames
,workbuf
,0); /* end of list */
2149 saalen
= ppubnames
->datalen
;
2150 pubnameslen
= saalen
+ 4;
2151 pubnamesbuf
= pbuf
= nasm_malloc(pubnameslen
);
2152 WRITELONG(pbuf
,saalen
); /* initial length */
2153 memcpy(pbuf
, saa_rbytes(ppubnames
, &saalen
), saalen
);
2154 saa_free(ppubnames
);
2156 /* build info section */
2157 pinfo
= saa_init(1L);
2158 WSAASHORT(pinfo
,workbuf
,3); /* dwarf version */
2159 WSAALONG(pinfo
,workbuf
,0); /* offset into abbrev */
2160 WSAACHAR(pinfo
,workbuf
,8); /* pointer size */
2161 WSAACHAR(pinfo
,workbuf
,1); /* abbrviation number LEB128u */
2162 WSAADLONG(pinfo
,workbuf
,0); /* DW_AT_low_pc */
2163 WSAADLONG(pinfo
,workbuf
,highaddr
); /* DW_AT_high_pc */
2164 WSAALONG(pinfo
,workbuf
,0); /* DW_AT_stmt_list */
2165 strcpy(workbuf
,elf_module
); /* input file name */
2166 saa_wbytes(pinfo
, workbuf
, (int32_t)(strlen(elf_module
) + 1));
2167 WSAACHAR(pinfo
,workbuf
,2); /* abbrviation number LEB128u */
2168 WSAADLONG(pinfo
,workbuf
,0); /* DW_AT_low_pc */
2169 WSAADLONG(pinfo
,workbuf
,0); /* DW_AT_frame_base */
2170 WSAACHAR(pinfo
,workbuf
,0); /* end of entries */
2171 saalen
= pinfo
->datalen
;
2172 infolen
= saalen
+ 4;
2173 infobuf
= pbuf
= nasm_malloc(infolen
);
2174 WRITELONG(pbuf
,saalen
); /* initial length */
2175 memcpy(pbuf
, saa_rbytes(pinfo
, &saalen
), saalen
);
2178 /* build rela.info section */
2180 inforelbuf
= pbuf
= nasm_malloc(inforellen
);
2181 WRITEDLONG(pbuf
, 12);
2182 WRITEDLONG(pbuf
, (2LL << 32) + R_X86_64_64
);
2183 WRITEDLONG(pbuf
, (uint64_t) 0);
2184 WRITEDLONG(pbuf
, 20);
2185 WRITEDLONG(pbuf
, (2LL << 32) + R_X86_64_64
);
2186 WRITEDLONG(pbuf
, (uint64_t) 0);
2188 /* build abbrev section */
2189 pabbrev
= saa_init(1L);
2190 WSAACHAR(pabbrev
,workbuf
,1); /* entry number LEB128u */
2191 WSAACHAR(pabbrev
,workbuf
,DW_TAG_compile_unit
); /* tag LEB128u */
2192 WSAACHAR(pabbrev
,workbuf
,1); /* has children */
2193 /* the following attributes and forms are all LEB128u values */
2194 WSAACHAR(pabbrev
,workbuf
,DW_AT_low_pc
);
2195 WSAACHAR(pabbrev
,workbuf
,DW_FORM_addr
);
2196 WSAACHAR(pabbrev
,workbuf
,DW_AT_high_pc
);
2197 WSAACHAR(pabbrev
,workbuf
,DW_FORM_addr
);
2198 WSAACHAR(pabbrev
,workbuf
,DW_AT_stmt_list
);
2199 WSAACHAR(pabbrev
,workbuf
,DW_FORM_data4
);
2200 WSAACHAR(pabbrev
,workbuf
,DW_AT_name
);
2201 WSAACHAR(pabbrev
,workbuf
,DW_FORM_string
);
2202 WSAASHORT(pabbrev
,workbuf
,0); /* end of entry */
2203 /* LEB128u usage same as above */
2204 WSAACHAR(pabbrev
,workbuf
,2); /* entry number */
2205 WSAACHAR(pabbrev
,workbuf
,DW_TAG_subprogram
);
2206 WSAACHAR(pabbrev
,workbuf
,0); /* no children */
2207 WSAACHAR(pabbrev
,workbuf
,DW_AT_low_pc
);
2208 WSAACHAR(pabbrev
,workbuf
,DW_FORM_addr
);
2209 WSAACHAR(pabbrev
,workbuf
,DW_AT_frame_base
);
2210 WSAACHAR(pabbrev
,workbuf
,DW_FORM_data4
);
2211 WSAASHORT(pabbrev
,workbuf
,0); /* end of entry */
2212 abbrevlen
= saalen
= pabbrev
->datalen
;
2213 abbrevbuf
= pbuf
= nasm_malloc(saalen
);
2214 memcpy(pbuf
, saa_rbytes(pabbrev
, &saalen
), saalen
);
2217 /* build line section */
2219 plines
= saa_init(1L);
2220 WSAACHAR(plines
,workbuf
,1); /* Minimum Instruction Length */
2221 WSAACHAR(plines
,workbuf
,1); /* Initial value of 'is_stmt' */
2222 WSAACHAR(plines
,workbuf
,line_base
); /* Line Base */
2223 WSAACHAR(plines
,workbuf
,line_range
); /* Line Range */
2224 WSAACHAR(plines
,workbuf
,opcode_base
); /* Opcode Base */
2225 /* standard opcode lengths (# of LEB128u operands) */
2226 WSAACHAR(plines
,workbuf
,0); /* Std opcode 1 length */
2227 WSAACHAR(plines
,workbuf
,1); /* Std opcode 2 length */
2228 WSAACHAR(plines
,workbuf
,1); /* Std opcode 3 length */
2229 WSAACHAR(plines
,workbuf
,1); /* Std opcode 4 length */
2230 WSAACHAR(plines
,workbuf
,1); /* Std opcode 5 length */
2231 WSAACHAR(plines
,workbuf
,0); /* Std opcode 6 length */
2232 WSAACHAR(plines
,workbuf
,0); /* Std opcode 7 length */
2233 WSAACHAR(plines
,workbuf
,0); /* Std opcode 8 length */
2234 WSAACHAR(plines
,workbuf
,1); /* Std opcode 9 length */
2235 WSAACHAR(plines
,workbuf
,0); /* Std opcode 10 length */
2236 WSAACHAR(plines
,workbuf
,0); /* Std opcode 11 length */
2237 WSAACHAR(plines
,workbuf
,1); /* Std opcode 12 length */
2238 /* Directory Table */
2239 WSAACHAR(plines
,workbuf
,0); /* End of table */
2240 /* File Name Table */
2241 ftentry
= dwarf_flist
;
2242 for (indx
= 0;indx
<dwarf_numfiles
;indx
++)
2244 saa_wbytes(plines
, ftentry
->filename
, (int32_t)(strlen(ftentry
->filename
) + 1));
2245 WSAACHAR(plines
,workbuf
,0); /* directory LEB128u */
2246 WSAACHAR(plines
,workbuf
,0); /* time LEB128u */
2247 WSAACHAR(plines
,workbuf
,0); /* size LEB128u */
2248 ftentry
= ftentry
->next
;
2250 WSAACHAR(plines
,workbuf
,0); /* End of table */
2251 linepoff
= plines
->datalen
;
2252 linelen
= linepoff
+ totlen
+ 10;
2253 linebuf
= pbuf
= nasm_malloc(linelen
);
2254 WRITELONG(pbuf
,linelen
-4); /* initial length */
2255 WRITESHORT(pbuf
,3); /* dwarf version */
2256 WRITELONG(pbuf
,linepoff
); /* offset to line number program */
2257 /* write line header */
2259 memcpy(pbuf
, saa_rbytes(plines
, &saalen
), saalen
);
2262 /* concatonate line program ranges */
2264 plinesrel
= saa_init(1L);
2265 psect
= dwarf_fsect
;
2266 for (indx
= 0; indx
< dwarf_nsections
; indx
++)
2268 WSAADLONG(plinesrel
,workbuf
, linepoff
);
2269 WSAADLONG(plinesrel
,workbuf
, ((uint64_t) (psect
->section
+ 2) << 32) + R_X86_64_64
);
2270 WSAADLONG(plinesrel
,workbuf
, (uint64_t) 0);
2271 plinep
= psect
->psaa
;
2272 saalen
= plinep
->datalen
;
2273 memcpy(pbuf
, saa_rbytes(plinep
, &saalen
), saalen
);
2277 /* done with this entry */
2278 psect
= psect
->next
;
2282 /* build rela.lines section */
2283 linerellen
=saalen
= plinesrel
->datalen
;
2284 linerelbuf
= pbuf
= nasm_malloc(linerellen
);
2285 memcpy(pbuf
, saa_rbytes(plinesrel
, &saalen
), saalen
);
2286 saa_free(plinesrel
);
2288 /* build frame section */
2290 framebuf
= pbuf
= nasm_malloc(framelen
);
2291 WRITELONG(pbuf
,framelen
-4); /* initial length */
2293 /* build loc section */
2295 locbuf
= pbuf
= nasm_malloc(loclen
);
2296 WRITEDLONG(pbuf
,0); /* null beginning offset */
2297 WRITEDLONG(pbuf
,0); /* null ending offset */
2300 void dwarf64_cleanup(void)
2303 nasm_free(arangesbuf
);
2305 nasm_free(arangesrelbuf
);
2307 nasm_free(pubnamesbuf
);
2311 nasm_free(inforelbuf
);
2313 nasm_free(abbrevbuf
);
2317 nasm_free(linerelbuf
);
2319 nasm_free(framebuf
);
2323 void dwarf64_findfile(const char * fname
)
2326 struct linelist
*match
;
2328 /* return if fname is current file name */
2329 if (dwarf_clist
&& !(strcmp(fname
, dwarf_clist
->filename
))) return;
2330 /* search for match */
2336 match
= dwarf_flist
;
2337 for (finx
= 0; finx
< dwarf_numfiles
; finx
++)
2339 if (!(strcmp(fname
, match
->filename
)))
2341 dwarf_clist
= match
;
2346 /* add file name to end of list */
2347 dwarf_clist
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
2349 dwarf_clist
->line
= dwarf_numfiles
;
2350 dwarf_clist
->filename
= nasm_malloc(sizeof(fname
+ 1));
2351 strcpy(dwarf_clist
->filename
,fname
);
2352 dwarf_clist
->next
= 0;
2353 /* if first entry */
2356 dwarf_flist
= dwarf_elist
= dwarf_clist
;
2357 dwarf_clist
->last
= 0;
2359 /* chain to previous entry */
2362 dwarf_elist
->next
= dwarf_clist
;
2363 dwarf_elist
= dwarf_clist
;
2368 void dwarf64_findsect(const int index
)
2371 struct sectlist
*match
;
2373 /* return if index is current section index */
2374 if (dwarf_csect
&& (dwarf_csect
->section
== index
))
2378 /* search for match */
2384 match
= dwarf_fsect
;
2385 for (sinx
= 0; sinx
< dwarf_nsections
; sinx
++)
2387 if ((match
->section
== index
))
2389 dwarf_csect
= match
;
2392 match
= match
->next
;
2395 /* add entry to end of list */
2396 dwarf_csect
= (struct sectlist
*)nasm_malloc(sizeof(struct sectlist
));
2398 dwarf_csect
->psaa
= plinep
= saa_init(1L);
2399 dwarf_csect
->line
= 1;
2400 dwarf_csect
->offset
= 0;
2401 dwarf_csect
->file
= 1;
2402 dwarf_csect
->section
= index
;
2403 dwarf_csect
->next
= 0;
2404 /* set relocatable address at start of line program */
2405 WSAACHAR(plinep
,workbuf
,DW_LNS_extended_op
);
2406 WSAACHAR(plinep
,workbuf
,9); /* operand length */
2407 WSAACHAR(plinep
,workbuf
,DW_LNE_set_address
);
2408 WSAADLONG(plinep
,workbuf
,0); /* Start Address */
2409 /* if first entry */
2412 dwarf_fsect
= dwarf_esect
= dwarf_csect
;
2413 dwarf_csect
->last
= 0;
2415 /* chain to previous entry */
2418 dwarf_esect
->next
= dwarf_csect
;
2419 dwarf_esect
= dwarf_csect
;
2424 /* write unsigned LEB128 value to SAA */
2425 void saa_wleb128u(struct SAA
*psaa
, int value
)
2427 char temp
[64], *ptemp
;
2437 if (value
!= 0) /* more bytes to come */
2442 } while (value
!= 0);
2443 saa_wbytes(psaa
, temp
, len
);
2445 /* write signed LEB128 value to SAA */
2446 void saa_wleb128s(struct SAA
*psaa
, int value
)
2448 char temp
[64], *ptemp
;
2450 bool more
, negative
;
2455 negative
= (value
< 0);
2456 size
= sizeof(int) * 8;
2460 byte
= value
& 0x7f;
2464 value
|= - (1 <<(size
- 7));
2465 /* sign bit of byte is second high order bit (0x40) */
2466 if ((value
== 0 && ! (byte
& 0x40)) ||
2467 ((value
== -1) && (byte
& 0x40)))
2475 saa_wbytes(psaa
, temp
, len
);