Add %ifempty and variants
[nasm/autotest.git] / output / outelf64.c
blob9cb4772888ab26b90a203ebcff65b5b70595a7fb
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.
8 */
9 #include "compiler.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <inttypes.h>
17 #include "nasm.h"
18 #include "nasmlib.h"
19 #include "stdscan.h"
20 #include "outform.h"
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
72 #if X86_MEMORY
74 #define WSAACHAR(s,p,v) \
75 do { \
76 *(uint8_t *)(p) = (v); \
77 saa_wbytes(s, p, 1); \
78 } while (0)
80 #define WSAASHORT(s,p,v) \
81 do { \
82 *(uint16_t *)(p) = (v); \
83 saa_wbytes(s, p, 2); \
84 } while (0)
86 #define WSAALONG(s,p,v) \
87 do { \
88 *(uint32_t *)(p) = (v); \
89 saa_wbytes(s, p, 4); \
90 } while (0)
92 #define WSAADLONG(s,p,v) \
93 do { \
94 *(uint64_t *)(p) = (v); \
95 saa_wbytes(s, p, 8); \
96 } while (0)
98 #define WSAAADDR(a,p,v,s) \
99 do { \
100 uint64_t _v = (v); \
101 memcpy((p), &_v, (s)); \
102 saa_wbytes(a, p, s); \
103 } while (0)
105 #else /* !X86_MEMORY */
107 #define WSAACHAR(s,p,v) \
108 do { \
109 *(uint8_t *)p = (v); \
110 saa_wbytes(s, p, 1); \
111 } while (0)
113 #define WSAASHORT(s,p,v) \
114 do { \
115 uint16_t _v = (v); \
116 uint8_t *_p = (uint8_t *)(p); \
117 _p[0] = _v; \
118 _p[1] = _v >> 8; \
119 saa_wbytes(s, _p, 2); \
120 } while (0)
122 #define WSAALONG(s,p,v) \
123 do { \
124 uint32_t _v = (v); \
125 uint8_t *_p = (uint8_t *)(p); \
126 _p[0] = _v; \
127 _p[1] = _v >> 8; \
128 _p[2] = _v >> 16; \
129 _p[3] = _v >> 24; \
130 saa_wbytes(s, _p, 4); \
131 } while (0)
133 #define WSAADLONG(s,p,v) \
134 do { \
135 uint64_t _v = (v); \
136 uint8_t *_p = (uint8_t *)(p); \
137 _p[0] = _v; \
138 _p[1] = _v >> 8; \
139 _p[2] = _v >> 16; \
140 _p[3] = _v >> 24; \
141 _p[4] = _v >> 32; \
142 _p[5] = _v >> 40; \
143 _p[6] = _v >> 48; \
144 _p[7] = _v >> 56; \
145 saa_wbytes(s, _p, 8); \
146 } while (0)
148 #define WSAAADDR(a,p,v,s) \
149 do { \
150 uint64_t _v = (v); \
151 uint8_t *_p = (uint8_t *)(p); \
152 _p[0] = _v; \
153 _p[1] = _v >> 8; \
154 _p[2] = _v >> 16; \
155 _p[3] = _v >> 24; \
156 _p[4] = _v >> 32; \
157 _p[5] = _v >> 40; \
158 _p[6] = _v >> 48; \
159 _p[7] = _v >> 56; \
160 saa_wbytes(a, _p, s); \
161 } while (0)
163 #endif
165 typedef uint32_t Elf64_Word;
166 typedef uint64_t Elf64_Xword;
167 typedef uint64_t Elf64_Addr;
168 typedef uint64_t Elf64_Off;
169 typedef struct
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 */
181 } Elf64_Shdr;
184 #ifdef OF_ELF64
187 struct Reloc {
188 struct Reloc *next;
189 int64_t address; /* relative to _start_ of section */
190 int64_t symbol; /* symbol index */
191 int type; /* type of relocation */
194 struct Symbol {
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 */
208 struct Section {
209 struct SAA *data;
210 uint64_t len, size;
211 uint32_t nrelocs;
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 */
216 char *name;
217 struct SAA *rel;
218 uint64_t rellen;
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;
241 static FILE *elffp;
242 static efunc error;
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;
254 #define SHN_UNDEF 0
256 #define SYM_GLOBAL 0x10
258 #define STV_DEFAULT 0
259 #define STV_INTERNAL 1
260 #define STV_HIDDEN 2
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 {
271 void *data;
272 int64_t len;
273 bool is_saa;
274 } *elf_sects;
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 *,
280 uint64_t);
281 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
282 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 */
291 #define N_BINCL 0x82
292 #define N_EINCL 0xA2
293 #define N_SLINE 0x44
294 #define TY_STABSSYMLIN 0x40 /* internal call to debug_out */
296 struct stabentry {
297 uint32_t n_strx;
298 uint8_t n_type;
299 uint8_t n_other;
300 uint16_t n_desc;
301 uint32_t n_value;
304 struct erel {
305 int offset, info;
308 struct symlininfo {
309 int offset;
310 int section; /* index into sects[] */
311 int segto; /* internal section number */
312 char *name; /* shallow-copied pointer of section name */
315 struct linelist {
316 struct symlininfo info;
317 int line;
318 char *filename;
319 struct linelist *next;
320 struct linelist *last;
323 struct sectlist {
324 struct SAA *psaa;
325 int section;
326 int line;
327 int offset;
328 int file;
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
387 * types.
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)
395 maxbits = 64;
396 elffp = fp;
397 error = errfunc;
398 evaluate = eval;
399 (void)ldef; /* placate optimisers */
400 sects = NULL;
401 nsects = sectlen = 0;
402 syms = saa_init((int32_t)sizeof(struct Symbol));
403 nlocals = nglobs = 0;
404 bsym = raa_init();
405 strs = saa_init(1L);
406 saa_wbytes(strs, "\0", 1L);
407 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
408 strslen = 2 + strlen(elf_module);
409 shstrtab = NULL;
410 shstrtablen = shstrtabsize = 0;;
411 add_sectname("", "");
413 fwds = NULL;
415 elf_gotpc_sect = seg_alloc();
416 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
417 error);
418 elf_gotoff_sect = seg_alloc();
419 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
420 error);
421 elf_got_sect = seg_alloc();
422 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
423 error);
424 elf_plt_sect = seg_alloc();
425 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
426 error);
427 elf_sym_sect = seg_alloc();
428 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
429 error);
431 def_seg = seg_alloc();
435 static void elf_cleanup(int debuginfo)
437 struct Reloc *r;
438 int i;
440 (void)debuginfo;
442 elf_write();
443 fclose(elffp);
444 for (i = 0; i < nsects; i++) {
445 if (sects[i]->type != SHT_NOBITS)
446 saa_free(sects[i]->data);
447 if (sects[i]->head)
448 saa_free(sects[i]->rel);
449 while (sects[i]->head) {
450 r = sects[i]->head;
451 sects[i]->head = sects[i]->head->next;
452 nasm_free(r);
455 nasm_free(sects);
456 saa_free(syms);
457 raa_free(bsym);
458 saa_free(strs);
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)
476 struct Section *s;
478 s = nasm_malloc(sizeof(*s));
480 if (type != SHT_NOBITS)
481 s->data = saa_init(1L);
482 s->head = NULL;
483 s->tail = &s->head;
484 s->len = s->size = 0;
485 s->nrelocs = 0;
486 if (!strcmp(name, ".text"))
487 s->index = def_seg;
488 else
489 s->index = seg_alloc();
490 add_sectname("", name);
491 s->name = nasm_malloc(1 + strlen(name));
492 strcpy(s->name, name);
493 s->type = type;
494 s->flags = flags;
495 s->align = align;
496 s->gsyms = NULL;
498 if (nsects >= sectlen)
499 sects =
500 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
501 sects[nsects++] = s;
503 return nsects - 1;
506 static int32_t elf_section_names(char *name, int pass, int *bits)
508 char *p;
509 unsigned flags_and, flags_or;
510 uint64_t type, align;
511 int i;
514 * Default is 64 bits.
516 if (!name) {
517 *bits = 64;
518 return def_seg;
521 p = name;
522 while (*p && !isspace(*p))
523 p++;
524 if (*p)
525 *p++ = '\0';
526 flags_and = flags_or = type = align = 0;
528 while (*p && isspace(*p))
529 p++;
530 while (*p) {
531 char *q = p;
532 while (*p && !isspace(*p))
533 p++;
534 if (*p)
535 *p++ = '\0';
536 while (*p && isspace(*p))
537 p++;
539 if (!nasm_strnicmp(q, "align=", 6)) {
540 align = atoi(q + 6);
541 if (align == 0)
542 align = 1;
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);
546 align = 1;
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")) {
567 type = SHT_PROGBITS;
568 } else if (!nasm_stricmp(q, "nobits")) {
569 type = SHT_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"
577 "name `%s'", name);
578 return NO_SEG;
581 for (i = 0; i < nsects; i++)
582 if (!strcmp(name, sects[i]->name))
583 break;
584 if (i == nsects) {
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);
596 else
597 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
598 if (type)
599 sects[i]->type = type;
600 if (align)
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)
618 int pos = strslen;
619 struct Symbol *sym;
620 bool special_used = false;
622 #if defined(DEBUG) && DEBUG>2
623 fprintf(stderr,
624 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
625 name, segment, offset, is_global, special);
626 #endif
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);
637 return;
640 if (is_global == 3) {
641 struct Symbol **s;
643 * Fix up a forward-reference symbol size from the first
644 * pass.
646 for (s = &fwds; *s; s = &(*s)->nextfwd)
647 if (!strcmp((*s)->name, name)) {
648 struct tokenval tokval;
649 expr *e;
650 char *p = special;
652 while (*p && !isspace(*p))
653 p++;
654 while (*p && isspace(*p))
655 p++;
656 stdscan_reset();
657 stdscan_bufptr = p;
658 tokval.t_type = TOKEN_INVALID;
659 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
660 if (e) {
661 if (!is_simple(e))
662 error(ERR_NONFATAL, "cannot use relocatable"
663 " expression as symbol size");
664 else
665 (*s)->size = reloc_value(e);
669 * Remove it from the list of unresolved sizes.
671 nasm_free((*s)->name);
672 *s = (*s)->nextfwd;
673 return;
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);
683 sym->strpos = pos;
684 sym->type = is_global ? SYM_GLOBAL : 0;
685 sym->other = STV_DEFAULT;
686 sym->size = 0;
687 if (segment == NO_SEG)
688 sym->section = SHN_ABS;
689 else {
690 int i;
691 sym->section = SHN_UNDEF;
692 if (nsects == 0 && segment == def_seg) {
693 int tempint;
694 if (segment != elf_section_names(".text", 2, &tempint))
695 error(ERR_PANIC,
696 "strange segment conditions in ELF driver");
697 sym->section = nsects;
698 } else {
699 for (i = 0; i < nsects; i++)
700 if (segment == sects[i]->index) {
701 sym->section = i + 1;
702 break;
707 if (is_global == 2) {
708 sym->size = offset;
709 sym->value = 0;
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.
716 if (special) {
717 bool err;
718 sym->value = readnum(special, &err);
719 if (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);
726 special_used = true;
727 } else
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:
736 * GLOBAL crash_nasm
737 * crash_nasm equ 0
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;
756 if (special) {
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;
766 else
767 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
768 n, special);
769 special += n;
771 while (isspace(*special))
772 ++special;
773 if (*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;
783 else
784 n = 0;
785 special += n;
788 if (*special) {
789 struct tokenval tokval;
790 expr *e;
791 int fwd = 0;
792 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
794 while (special[n] && isspace(special[n]))
795 n++;
797 * We have a size expression; attempt to
798 * evaluate it.
800 stdscan_reset();
801 stdscan_bufptr = special + n;
802 tokval.t_type = TOKEN_INVALID;
803 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
804 NULL);
805 if (fwd) {
806 sym->nextfwd = fwds;
807 fwds = sym;
808 sym->name = nasm_strdup(name);
809 } else if (e) {
810 if (!is_simple(e))
811 error(ERR_NONFATAL, "cannot use relocatable"
812 " expression as symbol size");
813 else
814 sym->size = reloc_value(e);
816 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
818 special_used = true;
821 sym->globnum = nglobs;
822 nglobs++;
823 } else
824 nlocals++;
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)
832 struct Reloc *r;
833 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
834 sect->tail = &r->next;
835 r->next = NULL;
837 r->address = sect->len;
838 if (segment == NO_SEG)
839 r->symbol = 0;
840 else {
841 int i;
842 r->symbol = 0;
843 for (i = 0; i < nsects; i++)
844 if (segment == sects[i]->index)
845 r->symbol = i + 2;
846 if (!r->symbol)
847 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
849 r->type = type;
851 sect->nrelocs++;
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)
880 struct Reloc *r;
881 struct Section *s;
882 struct Symbol *sym, *sm;
883 int i;
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.
892 s = NULL;
893 for (i = 0; i < nsects; i++)
894 if (segment == sects[i]->index) {
895 s = sects[i];
896 break;
898 if (!s) {
899 if (exact && offset != 0)
900 error(ERR_NONFATAL, "unable to find a suitable global symbol"
901 " for this reference");
902 else
903 elf_add_reloc(sect, segment, type);
904 return offset;
907 if (exact) {
909 * Find a symbol pointing _exactly_ at this one.
911 for (sym = s->gsyms; sym; sym = sym->next)
912 if (sym->value == offset)
913 break;
914 } else {
916 * Find the nearest symbol below this one.
918 sym = NULL;
919 for (sm = s->gsyms; sm; sm = sm->next)
920 if (sm->value <= offset && (!sym || sm->value > sym->value))
921 sym = sm;
923 if (!sym && exact) {
924 error(ERR_NONFATAL, "unable to find a suitable global symbol"
925 " for this reference");
926 return 0;
929 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
930 sect->tail = &r->next;
931 r->next = NULL;
933 r->address = sect->len;
934 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
935 r->type = type;
937 sect->nrelocs++;
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)
946 struct Section *s;
947 int64_t addr;
948 uint8_t mydata[16], *p;
949 int i;
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);
956 else fprintf(stderr,
957 " elf_out line: %d type: %x seg: %d segto: %d bytes: %x\n",
958 currentline, type, segment, segto, size);
959 #endif
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]"
967 " space");
968 return;
971 s = NULL;
972 for (i = 0; i < nsects; i++)
973 if (segto == sects[i]->index) {
974 s = sects[i];
975 break;
977 if (!s) {
978 int tempint; /* ignored */
979 if (segto != elf_section_names(".text", 2, &tempint))
980 error(ERR_PANIC, "strange segment conditions in ELF driver");
981 else {
982 s = sects[nsects - 1];
983 i = nsects - 1;
987 /* invoke current debug_output routine */
988 if (of_elf64.current_dfmt) {
989 sinfo.offset = s->len;
990 sinfo.section = i;
991 sinfo.segto = segto;
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)
1001 size = 2;
1002 else if (type == OUT_REL4ADR)
1003 size = 4;
1004 s->len += size;
1005 return;
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);
1013 } else
1014 s->len += 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) {
1020 bool gnu16 = false;
1021 addr = *(int64_t *)data;
1022 if (segment != NO_SEG) {
1023 if (segment % 2) {
1024 error(ERR_NONFATAL, "ELF format does not support"
1025 " segment base references");
1026 } else {
1027 if (wrt == NO_SEG) {
1028 switch ((int)size) {
1029 case 2:
1030 elf_add_reloc(s, segment, R_X86_64_16);
1031 break;
1032 case 4:
1033 elf_add_reloc(s, segment, R_X86_64_32);
1034 break;
1035 case 8:
1036 elf_add_reloc(s, segment, R_X86_64_64);
1037 break;
1038 default:
1039 error(ERR_PANIC, "internal error elf64-hpa-871");
1040 break;
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 $-$$.
1048 addr += s->len;
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) {
1057 case 2:
1058 gnu16 = true;
1059 addr = elf_add_gsym_reloc(s, segment, addr,
1060 R_X86_64_16, false);
1061 break;
1062 case 4:
1063 addr = elf_add_gsym_reloc(s, segment, addr,
1064 R_X86_64_32, false);
1065 break;
1066 case 8:
1067 addr = elf_add_gsym_reloc(s, segment, addr,
1068 R_X86_64_64, false);
1069 break;
1070 default:
1071 error(ERR_PANIC, "internal error elf64-hpa-903");
1072 break;
1074 } else if (wrt == elf_plt_sect + 1) {
1075 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
1076 "relative PLT references");
1077 } else {
1078 error(ERR_NONFATAL, "ELF format does not support this"
1079 " use of WRT");
1080 wrt = NO_SEG; /* we can at least _try_ to continue */
1084 p = mydata;
1085 if (gnu16) {
1086 WRITESHORT(p, addr);
1087 } else {
1088 if (size != 8 && size != 4 && segment != NO_SEG) {
1089 error(ERR_NONFATAL,
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");
1102 } else {
1103 if (wrt == NO_SEG) {
1104 elf_add_reloc(s, segment, R_X86_64_PC16);
1105 } else {
1106 error(ERR_NONFATAL,
1107 "Unsupported non-32-bit ELF relocation [2]");
1110 p = mydata;
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");
1119 } else {
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");
1129 } else {
1130 error(ERR_NONFATAL, "ELF format does not support this"
1131 " use of WRT");
1132 wrt = NO_SEG; /* we can at least _try_ to continue */
1135 p = mydata;
1136 WRITELONG(p, *(int64_t *)data - size);
1137 elf_sect_write(s, mydata, 4L);
1141 static void elf_write(void)
1143 int nsections, align;
1144 int scount;
1145 char *p;
1146 int commlen;
1147 char comment[64];
1148 int i;
1150 struct SAA *symtab;
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)
1160 nsections = 8;
1161 else if (of_elf64.current_dfmt == &df_dwarf)
1162 nsections = 15;
1163 else
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");
1205 * Do the comment.
1207 *comment = '\0';
1208 commlen =
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++)
1239 if (sects[i]->head)
1240 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1241 sects[i]->head);
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;
1249 elf_foffs += align;
1250 elf_nsect = 0;
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 */
1254 p = shstrtab + 1;
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);
1260 p += strlen(p) + 1;
1261 scount++; /* ditto */
1263 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1264 scount++; /* ditto */
1265 p += strlen(p) + 1;
1266 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1267 scount++; /* ditto */
1268 p += strlen(p) + 1;
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 */
1271 p += strlen(p) + 1;
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) {
1275 p += strlen(p) + 1;
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 */
1284 stabs64_generate();
1286 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1287 p += strlen(p) + 1;
1288 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1289 nsections - 2, 0, 4, 12);
1291 p += strlen(p) + 1;
1292 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1293 stabstrlen, 0, 0, 4, 0);
1295 p += strlen(p) + 1;
1296 /* link -> symtable info -> section to refer to */
1297 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1298 stabrellen, symtabsection, nsections - 3, 4,
1299 16);
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 */
1306 dwarf64_generate();
1308 p += strlen(p) + 1;
1309 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1310 arangeslen, 0, 0, 1, 0);
1311 p += strlen(p) + 1;
1312 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1313 arangesrellen, symtabsection, debug_aranges, 1, 24);
1314 p += strlen(p) + 1;
1315 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1316 pubnameslen, 0, 0, 1, 0);
1317 p += strlen(p) + 1;
1318 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1319 infolen, 0, 0, 1, 0);
1320 p += strlen(p) + 1;
1321 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1322 inforellen, symtabsection, debug_info, 1, 24);
1323 p += strlen(p) + 1;
1324 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1325 abbrevlen, 0, 0, 1, 0);
1326 p += strlen(p) + 1;
1327 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1328 linelen, 0, 0, 1, 0);
1329 p += strlen(p) + 1;
1330 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1331 linerellen, symtabsection, debug_line, 1, 24);
1332 p += strlen(p) + 1;
1333 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1334 framelen, 0, 0, 8, 0);
1335 p += strlen(p) + 1;
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);
1348 saa_free(symtab);
1351 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1353 struct SAA *s = saa_init(1L);
1354 struct Symbol *sym;
1355 uint8_t entry[24], *p;
1356 int i;
1358 *len = *local = 0;
1361 * First, an all-zeros entry, required by the ELF spec.
1363 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1364 *len += 24;
1365 (*local)++;
1368 * Next, an entry for the file name.
1370 p = entry;
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);
1377 *len += 24;
1378 (*local)++;
1381 * Now some standard symbols defining the segments, for relocation
1382 * purposes.
1384 for (i = 1; i <= nsects; i++) {
1385 p = entry;
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);
1392 *len += 24;
1393 (*local)++;
1397 * Now the other local symbols.
1399 saa_rewind(syms);
1400 while ((sym = saa_rstruct(syms))) {
1401 if (sym->type & SYM_GLOBAL)
1402 continue;
1403 p = entry;
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);
1411 *len += 24;
1412 (*local)++;
1416 * Now the global symbols.
1418 saa_rewind(syms);
1419 while ((sym = saa_rstruct(syms))) {
1420 if (!(sym->type & SYM_GLOBAL))
1421 continue;
1422 p = entry;
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);
1430 *len += 24;
1433 return s;
1436 static struct SAA *elf_build_reltab(uint64_t *len, struct Reloc *r)
1438 struct SAA *s;
1439 uint8_t *p, entry[24];
1441 if (!r)
1442 return NULL;
1444 s = saa_init(1L);
1445 *len = 0;
1447 while (r) {
1448 int64_t sym = r->symbol;
1450 if (sym >= GLOBAL_TEMP_BASE)
1451 sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1453 p = entry;
1454 WRITEDLONG(p, r->address);
1455 WRITEDLONG(p, (sym << 32) + r->type);
1456 WRITEDLONG(p, (uint64_t) 0);
1457 saa_wbytes(s, entry, 24L);
1458 *len += 24;
1460 r = r->next;
1463 return s;
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;
1473 elf_nsect++;
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);
1481 if (data)
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)
1491 int i;
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);
1499 else
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);
1509 sect->len += len;
1512 static int32_t elf_segbase(int32_t segment)
1514 return segment;
1517 static int elf_directive(char *directive, char *value, int pass)
1519 bool err;
1520 int64_t n;
1521 char *p;
1523 if (!strcmp(directive, "osabi")) {
1524 if (pass == 2)
1525 return 1; /* ignore in pass 2 */
1527 n = readnum(value, &err);
1528 if (err) {
1529 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1530 return 1;
1532 if (n < 0 || n > 255) {
1533 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1534 return 1;
1536 elf_osabi = n;
1537 elf_abiver = 0;
1539 if ((p = strchr(value,',')) == NULL)
1540 return 1;
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)");
1545 return 1;
1548 elf_abiver = n;
1549 return 1;
1552 return 0;
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",
1564 "%define $_%1 $%1",
1565 "%endmacro",
1566 "%macro osabi 1+.nolist",
1567 "[osabi %1]",
1568 "%endmacro",
1569 NULL
1571 static int elf_set_info(enum geninfo type, char **val)
1573 (void)type;
1574 (void)val;
1575 return 0;
1577 static struct dfmt df_dwarf = {
1578 "ELF64 (X86_64) dwarf debug format for Linux",
1579 "dwarf",
1580 dwarf64_init,
1581 dwarf64_linenum,
1582 dwarf64_deflabel,
1583 dwarf64_directive,
1584 dwarf64_typevalue,
1585 dwarf64_output,
1586 dwarf64_cleanup
1588 static struct dfmt df_stabs = {
1589 "ELF64 (X86_64) stabs debug format for Linux",
1590 "stabs",
1591 stabs64_init,
1592 stabs64_linenum,
1593 stabs64_deflabel,
1594 stabs64_directive,
1595 stabs64_typevalue,
1596 stabs64_output,
1597 stabs64_cleanup
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)",
1604 "elf64",
1605 NULL,
1606 elf64_debugs_arr,
1607 &null_debug_form,
1608 elf_stdmac,
1609 elf_init,
1610 elf_set_info,
1611 elf_out,
1612 elf_deflabel,
1613 elf_section_names,
1614 elf_segbase,
1615 elf_directive,
1616 elf_filename,
1617 elf_cleanup
1620 /* again, the stabs debugging stuff (code) */
1622 void stabs64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1624 (void)of;
1625 (void)id;
1626 (void)fp;
1627 (void)error;
1630 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1632 (void)segto;
1633 if (!stabs_filename) {
1634 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1635 strcpy(stabs_filename, filename);
1636 } else {
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);
1648 stabs_immcall = 1;
1649 currentline = linenumber;
1652 void stabs64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1653 char *special)
1655 (void)name;
1656 (void)segment;
1657 (void)offset;
1658 (void)is_global;
1659 (void)special;
1662 void stabs64_directive(const char *directive, const char *params)
1664 (void)directive;
1665 (void)params;
1668 void stabs64_typevalue(int32_t type)
1670 int32_t stype, ssize;
1671 switch (TYM_TYPE(type)) {
1672 case TY_LABEL:
1673 ssize = 0;
1674 stype = STT_NOTYPE;
1675 break;
1676 case TY_BYTE:
1677 ssize = 1;
1678 stype = STT_OBJECT;
1679 break;
1680 case TY_WORD:
1681 ssize = 2;
1682 stype = STT_OBJECT;
1683 break;
1684 case TY_DWORD:
1685 ssize = 4;
1686 stype = STT_OBJECT;
1687 break;
1688 case TY_FLOAT:
1689 ssize = 4;
1690 stype = STT_OBJECT;
1691 break;
1692 case TY_QWORD:
1693 ssize = 8;
1694 stype = STT_OBJECT;
1695 break;
1696 case TY_TBYTE:
1697 ssize = 10;
1698 stype = STT_OBJECT;
1699 break;
1700 case TY_OWORD:
1701 ssize = 16;
1702 stype = STT_OBJECT;
1703 break;
1704 case TY_COMMON:
1705 ssize = 0;
1706 stype = STT_COMMON;
1707 break;
1708 case TY_SEG:
1709 ssize = 0;
1710 stype = STT_SECTION;
1711 break;
1712 case TY_EXTERN:
1713 ssize = 0;
1714 stype = STT_NOTYPE;
1715 break;
1716 case TY_EQU:
1717 ssize = 0;
1718 stype = STT_NOTYPE;
1719 break;
1720 default:
1721 ssize = 0;
1722 stype = STT_NOTYPE;
1723 break;
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 */
1740 numlinestabs++;
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;
1747 el->next = 0;
1748 if (stabslines) {
1749 stabslines->last->next = el;
1750 stabslines->last = el;
1751 } else {
1752 stabslines = el;
1753 stabslines->last = el;
1757 stabs_immcall = 0;
1760 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1761 do {\
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); \
1767 } while (0)
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;
1775 char **allfiles;
1776 int *fileidx;
1778 struct linelist *ptr;
1780 ptr = stabslines;
1782 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1783 for (i = 0; i < numlinestabs; i++)
1784 allfiles[i] = 0;
1785 numfiles = 0;
1786 while (ptr) {
1787 if (numfiles == 0) {
1788 allfiles[0] = ptr->filename;
1789 numfiles++;
1790 } else {
1791 for (i = 0; i < numfiles; i++) {
1792 if (!strcmp(allfiles[i], ptr->filename))
1793 break;
1795 if (i >= numfiles) {
1796 allfiles[i] = ptr->filename;
1797 numfiles++;
1800 ptr = ptr->next;
1802 strsize = 1;
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;
1808 mainfileindex = 0;
1809 for (i = 0; i < numfiles; i++) {
1810 if (!strcmp(allfiles[i], elf_module)) {
1811 mainfileindex = i;
1812 break;
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
1819 sbuf =
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));
1826 rptr = rbuf;
1828 for (i = 0; i < numfiles; i++) {
1829 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1831 ssbuf[0] = 0;
1833 stabstrlen = strsize; /* set global variable for length of stab strings */
1835 sptr = sbuf;
1836 ptr = stabslines;
1837 numstabs = 0;
1839 if (ptr) {
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
1842 of remaining stabs
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);
1859 numstabs++;
1860 currfile = mainfileindex;
1863 while (ptr) {
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))
1868 break;
1869 currfile = i;
1870 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1871 ptr->info.offset);
1872 numstabs++;
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);
1882 numstabs++;
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);
1890 ptr = ptr->next;
1894 ((struct stabentry *)sbuf)->n_desc = numstabs;
1896 nasm_free(allfiles);
1897 nasm_free(fileidx);
1899 stablen = (sptr - sbuf);
1900 stabrellen = (rptr - rbuf);
1901 stabrelbuf = rbuf;
1902 stabbuf = sbuf;
1903 stabstrbuf = ssbuf;
1906 void stabs64_cleanup(void)
1908 struct linelist *ptr, *del;
1909 if (!stabslines)
1910 return;
1911 ptr = stabslines;
1912 while (ptr) {
1913 del = ptr;
1914 ptr = ptr->next;
1915 nasm_free(del);
1917 if (stabbuf)
1918 nasm_free(stabbuf);
1919 if (stabrelbuf)
1920 nasm_free(stabrelbuf);
1921 if (stabstrbuf)
1922 nasm_free(stabstrbuf);
1924 /* dwarf routines */
1926 void dwarf64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1928 (void)of;
1929 (void)id;
1930 (void)fp;
1931 (void)error;
1934 void dwarf64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1936 (void)segto;
1937 dwarf64_findfile(filename);
1938 dwarf_immcall = 1;
1939 currentline = linenumber;
1942 void dwarf64_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1943 char *special)
1945 (void)name;
1946 (void)segment;
1947 (void)offset;
1948 (void)is_global;
1949 (void)special;
1952 void dwarf64_directive(const char *directive, const char *params)
1954 (void)directive;
1955 (void)params;
1958 void dwarf64_typevalue(int32_t type)
1960 int32_t stype, ssize;
1961 switch (TYM_TYPE(type)) {
1962 case TY_LABEL:
1963 ssize = 0;
1964 stype = STT_NOTYPE;
1965 break;
1966 case TY_BYTE:
1967 ssize = 1;
1968 stype = STT_OBJECT;
1969 break;
1970 case TY_WORD:
1971 ssize = 2;
1972 stype = STT_OBJECT;
1973 break;
1974 case TY_DWORD:
1975 ssize = 4;
1976 stype = STT_OBJECT;
1977 break;
1978 case TY_FLOAT:
1979 ssize = 4;
1980 stype = STT_OBJECT;
1981 break;
1982 case TY_QWORD:
1983 ssize = 8;
1984 stype = STT_OBJECT;
1985 break;
1986 case TY_TBYTE:
1987 ssize = 10;
1988 stype = STT_OBJECT;
1989 break;
1990 case TY_OWORD:
1991 ssize = 16;
1992 stype = STT_OBJECT;
1993 break;
1994 case TY_COMMON:
1995 ssize = 0;
1996 stype = STT_COMMON;
1997 break;
1998 case TY_SEG:
1999 ssize = 0;
2000 stype = STT_SECTION;
2001 break;
2002 case TY_EXTERN:
2003 ssize = 0;
2004 stype = STT_NOTYPE;
2005 break;
2006 case TY_EQU:
2007 ssize = 0;
2008 stype = STT_NOTYPE;
2009 break;
2010 default:
2011 ssize = 0;
2012 stype = STT_NOTYPE;
2013 break;
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;
2025 struct SAA *plinep;
2027 (void)type;
2029 s = (struct symlininfo *)param;
2030 /* line number info is only gathered for executable sections */
2031 if (!(sects[s->section]->flags & SHF_EXECINSTR))
2032 return;
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 */
2039 if (dwarf_immcall)
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 */
2053 if (ln)
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);
2062 else
2064 if (ln)
2066 WSAACHAR(plinep,workbuf,DW_LNS_advance_line);
2067 saa_wleb128s(plinep,ln);
2069 if (aa)
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 */
2079 dwarf_immcall = 0;
2084 void dwarf64_generate(void)
2086 uint8_t *pbuf;
2087 int indx;
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;
2105 totlen = 0;
2106 highaddr = 0;
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);
2135 saa_free(paranges);
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);
2176 saa_free(pinfo);
2178 /* build rela.info section */
2179 inforellen = 48;
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);
2215 saa_free(pabbrev);
2217 /* build line section */
2218 /* prolog */
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 */
2258 saalen = linepoff;
2259 memcpy(pbuf, saa_rbytes(plines, &saalen), saalen);
2260 pbuf += linepoff;
2261 saa_free(plines);
2262 /* concatonate line program ranges */
2263 linepoff += 13;
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);
2274 pbuf += saalen;
2275 linepoff += saalen;
2276 saa_free(plinep);
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 */
2289 framelen = 4;
2290 framebuf = pbuf = nasm_malloc(framelen);
2291 WRITELONG(pbuf,framelen-4); /* initial length */
2293 /* build loc section */
2294 loclen = 16;
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)
2302 if (arangesbuf)
2303 nasm_free(arangesbuf);
2304 if (arangesrelbuf)
2305 nasm_free(arangesrelbuf);
2306 if (pubnamesbuf)
2307 nasm_free(pubnamesbuf);
2308 if (infobuf)
2309 nasm_free(infobuf);
2310 if (inforelbuf)
2311 nasm_free(inforelbuf);
2312 if (abbrevbuf)
2313 nasm_free(abbrevbuf);
2314 if (linebuf)
2315 nasm_free(linebuf);
2316 if (linerelbuf)
2317 nasm_free(linerelbuf);
2318 if (framebuf)
2319 nasm_free(framebuf);
2320 if (locbuf)
2321 nasm_free(locbuf);
2323 void dwarf64_findfile(const char * fname)
2325 int finx;
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 */
2331 else
2333 match = 0;
2334 if (dwarf_flist)
2336 match = dwarf_flist;
2337 for (finx = 0; finx < dwarf_numfiles; finx++)
2339 if (!(strcmp(fname, match->filename)))
2341 dwarf_clist = match;
2342 return;
2346 /* add file name to end of list */
2347 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2348 dwarf_numfiles++;
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 */
2354 if (!dwarf_flist)
2356 dwarf_flist = dwarf_elist = dwarf_clist;
2357 dwarf_clist->last = 0;
2359 /* chain to previous entry */
2360 else
2362 dwarf_elist->next = dwarf_clist;
2363 dwarf_elist = dwarf_clist;
2367 /* */
2368 void dwarf64_findsect(const int index)
2370 int sinx;
2371 struct sectlist *match;
2372 struct SAA *plinep;
2373 /* return if index is current section index */
2374 if (dwarf_csect && (dwarf_csect->section == index))
2376 return;
2378 /* search for match */
2379 else
2381 match = 0;
2382 if (dwarf_fsect)
2384 match = dwarf_fsect;
2385 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2387 if ((match->section == index))
2389 dwarf_csect = match;
2390 return;
2392 match = match->next;
2395 /* add entry to end of list */
2396 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2397 dwarf_nsections++;
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 */
2410 if (!dwarf_fsect)
2412 dwarf_fsect = dwarf_esect = dwarf_csect;
2413 dwarf_csect->last = 0;
2415 /* chain to previous entry */
2416 else
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;
2428 uint8_t byte;
2429 int len;
2431 ptemp = temp;
2432 len = 0;
2435 byte = value & 127;
2436 value >>= 7;
2437 if (value != 0) /* more bytes to come */
2438 byte |= 0x80;
2439 *ptemp = byte;
2440 ptemp++;
2441 len++;
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;
2449 uint8_t byte;
2450 bool more, negative;
2451 int size, len;
2453 ptemp = temp;
2454 more = 1;
2455 negative = (value < 0);
2456 size = sizeof(int) * 8;
2457 len = 0;
2458 while(more)
2460 byte = value & 0x7f;
2461 value >>= 7;
2462 if (negative)
2463 /* sign extend */
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)))
2468 more = 0;
2469 else
2470 byte |= 0x80;
2471 *ptemp = byte;
2472 ptemp++;
2473 len++;
2475 saa_wbytes(psaa, temp, len);
2477 #endif /* OF_ELF */