Move declarations before statements
[nasm.git] / output / outelf64.c
blob2ef1de35f06b6fc9d49af61d37ee7ba747e25aa4
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 licence given in the file "Licence"
7 * distributed in the NASM archive.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "stdscan.h"
21 #include "outform.h"
23 /* Definitions in lieu of elf.h */
25 #define SHT_PROGBITS 1
26 #define SHT_RELA 4 /* Relocation entries with addends */
27 #define SHT_NOBITS 8
28 #define SHF_WRITE (1 << 0) /* Writable */
29 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
30 #define SHF_EXECINSTR (1 << 2) /* Executable */
31 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
32 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
33 #define R_X86_64_NONE 0 /* No reloc */
34 #define R_X86_64_64 1 /* Direct 64 bit address */
35 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
36 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
37 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
38 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
39 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
40 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
41 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
42 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
43 #define ET_REL 1 /* Relocatable file */
44 #define EM_X86_64 62 /* AMD x86-64 architecture */
45 #define STT_NOTYPE 0 /* Symbol type is unspecified */
46 #define STT_OBJECT 1 /* Symbol is a data object */
47 #define STT_FUNC 2 /* Symbol is a code object */
48 #define STT_SECTION 3 /* Symbol associated with a section */
49 #define STT_FILE 4 /* Symbol's name is file name */
50 #define STT_COMMON 5 /* Symbol is a common data object */
51 #define STT_TLS 6 /* Symbol is thread-local data object*/
52 #define STT_NUM 7 /* Number of defined types. */
53 typedef uint32_t Elf64_Word;
54 typedef uint64_t Elf64_Xword;
55 typedef uint64_t Elf64_Addr;
56 typedef uint64_t Elf64_Off;
57 typedef struct
59 Elf64_Word sh_name; /* Section name (string tbl index) */
60 Elf64_Word sh_type; /* Section type */
61 Elf64_Xword sh_flags; /* Section flags */
62 Elf64_Addr sh_addr; /* Section virtual addr at execution */
63 Elf64_Off sh_offset; /* Section file offset */
64 Elf64_Xword sh_size; /* Section size in bytes */
65 Elf64_Word sh_link; /* Link to another section */
66 Elf64_Word sh_info; /* Additional section information */
67 Elf64_Xword sh_addralign; /* Section alignment */
68 Elf64_Xword sh_entsize; /* Entry size if section holds table */
69 } Elf64_Shdr;
72 #ifdef OF_ELF64
75 struct Reloc {
76 struct Reloc *next;
77 int64_t address; /* relative to _start_ of section */
78 int64_t symbol; /* symbol index */
79 int type; /* type of relocation */
82 struct Symbol {
83 int32_t strpos; /* string table position of name */
84 int32_t section; /* section ID of the symbol */
85 int type; /* symbol type */
86 int other; /* symbol visibility */
87 int64_t value; /* address, or COMMON variable align */
88 int32_t size; /* size of symbol */
89 int32_t globnum; /* symbol table offset if global */
90 struct Symbol *next; /* list of globals in each section */
91 struct Symbol *nextfwd; /* list of unresolved-size symbols */
92 char *name; /* used temporarily if in above list */
96 struct Section {
97 struct SAA *data;
98 uint32_t len, size, nrelocs;
99 int32_t index;
100 int type; /* SHT_PROGBITS or SHT_NOBITS */
101 int align; /* alignment: power of two */
102 uint32_t flags; /* section flags */
103 char *name;
104 struct SAA *rel;
105 int32_t rellen;
106 struct Reloc *head, **tail;
107 struct Symbol *gsyms; /* global symbols in section */
110 #define SECT_DELTA 32
111 static struct Section **sects;
112 static int nsects, sectlen;
114 #define SHSTR_DELTA 256
115 static char *shstrtab;
116 static int shstrtablen, shstrtabsize;
118 static struct SAA *syms;
119 static uint32_t nlocals, nglobs;
121 static int32_t def_seg;
123 static struct RAA *bsym;
125 static struct SAA *strs;
126 static uint32_t strslen;
128 static FILE *elffp;
129 static efunc error;
130 static evalfunc evaluate;
132 static struct Symbol *fwds;
134 static char elf_module[FILENAME_MAX];
136 extern struct ofmt of_elf64;
138 #define SHN_UNDEF 0
140 #define SYM_GLOBAL 0x10
142 #define STV_DEFAULT 0
143 #define STV_INTERNAL 1
144 #define STV_HIDDEN 2
145 #define STV_PROTECTED 3
147 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
149 #define SEG_ALIGN 16 /* alignment of sections in file */
150 #define SEG_ALIGN_1 (SEG_ALIGN-1)
152 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
154 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
155 static struct ELF_SECTDATA {
156 void *data;
157 int32_t len;
158 bool is_saa;
159 } *elf_sects;
160 static int elf_nsect;
161 static int32_t elf_foffs;
163 static void elf_write(void);
164 static void elf_sect_write(struct Section *, const uint8_t *,
165 uint32_t);
166 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
167 int, int);
168 static void elf_write_sections(void);
169 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
170 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
171 static void add_sectname(char *, char *);
173 /* this stuff is needed for the stabs debugging format */
174 #define N_SO 0x64 /* ID for main source file */
175 #define N_SOL 0x84 /* ID for sub-source file */
176 #define N_BINCL 0x82
177 #define N_EINCL 0xA2
178 #define N_SLINE 0x44
179 #define TY_STABSSYMLIN 0x40 /* ouch */
181 struct stabentry {
182 uint32_t n_strx;
183 uint8_t n_type;
184 uint8_t n_other;
185 uint16_t n_desc;
186 uint32_t n_value;
189 struct erel {
190 int offset, info;
193 struct symlininfo {
194 int offset;
195 int section; /* section index */
196 char *name; /* shallow-copied pointer of section name */
199 struct linelist {
200 struct symlininfo info;
201 int line;
202 char *filename;
203 struct linelist *next;
204 struct linelist *last;
207 static struct linelist *stabslines = 0;
208 static int stabs_immcall = 0;
209 static int currentline = 0;
210 static int numlinestabs = 0;
211 static char *stabs_filename = 0;
212 static int symtabsection;
213 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
214 static int stablen, stabstrlen, stabrellen;
216 static struct dfmt df_stabs;
217 static struct Symbol *lastsym;
219 void stabs64_init(struct ofmt *, void *, FILE *, efunc);
220 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
221 void stabs64_deflabel(char *, int32_t, int32_t, int, char *);
222 void stabs64_directive(const char *, const char *);
223 void stabs64_typevalue(int32_t);
224 void stabs64_output(int, void *);
225 void stabs64_generate(void);
226 void stabs64_cleanup(void);
228 /* end of stabs debugging stuff */
231 * Special section numbers which are used to define ELF special
232 * symbols, which can be used with WRT to provide PIC relocation
233 * types.
235 static int32_t elf_gotpc_sect, elf_gotoff_sect;
236 static int32_t elf_got_sect, elf_plt_sect;
237 static int32_t elf_sym_sect;
239 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
241 maxbits = 64;
242 elffp = fp;
243 error = errfunc;
244 evaluate = eval;
245 (void)ldef; /* placate optimisers */
246 sects = NULL;
247 nsects = sectlen = 0;
248 syms = saa_init((int32_t)sizeof(struct Symbol));
249 nlocals = nglobs = 0;
250 bsym = raa_init();
251 strs = saa_init(1L);
252 saa_wbytes(strs, "\0", 1L);
253 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
254 strslen = 2 + strlen(elf_module);
255 shstrtab = NULL;
256 shstrtablen = shstrtabsize = 0;;
257 add_sectname("", "");
259 fwds = NULL;
261 elf_gotpc_sect = seg_alloc();
262 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
263 error);
264 elf_gotoff_sect = seg_alloc();
265 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
266 error);
267 elf_got_sect = seg_alloc();
268 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
269 error);
270 elf_plt_sect = seg_alloc();
271 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
272 error);
273 elf_sym_sect = seg_alloc();
274 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
275 error);
277 def_seg = seg_alloc();
280 static void elf_cleanup(int debuginfo)
282 struct Reloc *r;
283 int i;
285 (void)debuginfo;
287 elf_write();
288 fclose(elffp);
289 for (i = 0; i < nsects; i++) {
290 if (sects[i]->type != SHT_NOBITS)
291 saa_free(sects[i]->data);
292 if (sects[i]->head)
293 saa_free(sects[i]->rel);
294 while (sects[i]->head) {
295 r = sects[i]->head;
296 sects[i]->head = sects[i]->head->next;
297 nasm_free(r);
300 nasm_free(sects);
301 saa_free(syms);
302 raa_free(bsym);
303 saa_free(strs);
304 if (of_elf64.current_dfmt) {
305 of_elf64.current_dfmt->cleanup();
309 static void add_sectname(char *firsthalf, char *secondhalf)
311 int len = strlen(firsthalf) + strlen(secondhalf);
312 while (shstrtablen + len + 1 > shstrtabsize)
313 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
314 strcpy(shstrtab + shstrtablen, firsthalf);
315 strcat(shstrtab + shstrtablen, secondhalf);
316 shstrtablen += len + 1;
319 static int elf_make_section(char *name, int type, int flags, int align)
321 struct Section *s;
323 s = nasm_malloc(sizeof(*s));
325 if (type != SHT_NOBITS)
326 s->data = saa_init(1L);
327 s->head = NULL;
328 s->tail = &s->head;
329 s->len = s->size = 0;
330 s->nrelocs = 0;
331 if (!strcmp(name, ".text"))
332 s->index = def_seg;
333 else
334 s->index = seg_alloc();
335 add_sectname("", name);
336 s->name = nasm_malloc(1 + strlen(name));
337 strcpy(s->name, name);
338 s->type = type;
339 s->flags = flags;
340 s->align = align;
341 s->gsyms = NULL;
343 if (nsects >= sectlen)
344 sects =
345 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
346 sects[nsects++] = s;
348 return nsects - 1;
351 static int32_t elf_section_names(char *name, int pass, int *bits)
353 char *p;
354 unsigned flags_and, flags_or;
355 int type, align, i;
358 * Default is 64 bits.
360 if (!name) {
361 *bits = 64;
362 return def_seg;
365 p = name;
366 while (*p && !isspace(*p))
367 p++;
368 if (*p)
369 *p++ = '\0';
370 flags_and = flags_or = type = align = 0;
372 while (*p && isspace(*p))
373 p++;
374 while (*p) {
375 char *q = p;
376 while (*p && !isspace(*p))
377 p++;
378 if (*p)
379 *p++ = '\0';
380 while (*p && isspace(*p))
381 p++;
383 if (!nasm_strnicmp(q, "align=", 6)) {
384 align = atoi(q + 6);
385 if (align == 0)
386 align = 1;
387 if ((align - 1) & align) { /* means it's not a power of two */
388 error(ERR_NONFATAL, "section alignment %d is not"
389 " a power of two", align);
390 align = 1;
392 } else if (!nasm_stricmp(q, "alloc")) {
393 flags_and |= SHF_ALLOC;
394 flags_or |= SHF_ALLOC;
395 } else if (!nasm_stricmp(q, "noalloc")) {
396 flags_and |= SHF_ALLOC;
397 flags_or &= ~SHF_ALLOC;
398 } else if (!nasm_stricmp(q, "exec")) {
399 flags_and |= SHF_EXECINSTR;
400 flags_or |= SHF_EXECINSTR;
401 } else if (!nasm_stricmp(q, "noexec")) {
402 flags_and |= SHF_EXECINSTR;
403 flags_or &= ~SHF_EXECINSTR;
404 } else if (!nasm_stricmp(q, "write")) {
405 flags_and |= SHF_WRITE;
406 flags_or |= SHF_WRITE;
407 } else if (!nasm_stricmp(q, "nowrite")) {
408 flags_and |= SHF_WRITE;
409 flags_or &= ~SHF_WRITE;
410 } else if (!nasm_stricmp(q, "progbits")) {
411 type = SHT_PROGBITS;
412 } else if (!nasm_stricmp(q, "nobits")) {
413 type = SHT_NOBITS;
417 if (!strcmp(name, ".comment") ||
418 !strcmp(name, ".shstrtab") ||
419 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
420 error(ERR_NONFATAL, "attempt to redefine reserved section"
421 "name `%s'", name);
422 return NO_SEG;
425 for (i = 0; i < nsects; i++)
426 if (!strcmp(name, sects[i]->name))
427 break;
428 if (i == nsects) {
429 if (!strcmp(name, ".text"))
430 i = elf_make_section(name, SHT_PROGBITS,
431 SHF_ALLOC | SHF_EXECINSTR, 16);
432 else if (!strcmp(name, ".rodata"))
433 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
434 else if (!strcmp(name, ".data"))
435 i = elf_make_section(name, SHT_PROGBITS,
436 SHF_ALLOC | SHF_WRITE, 4);
437 else if (!strcmp(name, ".bss"))
438 i = elf_make_section(name, SHT_NOBITS,
439 SHF_ALLOC | SHF_WRITE, 4);
440 else
441 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
442 if (type)
443 sects[i]->type = type;
444 if (align)
445 sects[i]->align = align;
446 sects[i]->flags &= ~flags_and;
447 sects[i]->flags |= flags_or;
448 } else if (pass == 1) {
449 if ((type && sects[i]->type != type)
450 || (align && sects[i]->align != align)
451 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
452 error(ERR_WARNING, "incompatible section attributes ignored on"
453 " redeclaration of section `%s'", name);
456 return sects[i]->index;
459 static void elf_deflabel(char *name, int32_t segment, int32_t offset,
460 int is_global, char *special)
462 int pos = strslen;
463 struct Symbol *sym;
464 bool special_used = false;
466 #if defined(DEBUG) && DEBUG>2
467 fprintf(stderr,
468 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
469 name, segment, offset, is_global, special);
470 #endif
471 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
473 * This is a NASM special symbol. We never allow it into
474 * the ELF symbol table, even if it's a valid one. If it
475 * _isn't_ a valid one, we should barf immediately.
477 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
478 strcmp(name, "..got") && strcmp(name, "..plt") &&
479 strcmp(name, "..sym"))
480 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
481 return;
484 if (is_global == 3) {
485 struct Symbol **s;
487 * Fix up a forward-reference symbol size from the first
488 * pass.
490 for (s = &fwds; *s; s = &(*s)->nextfwd)
491 if (!strcmp((*s)->name, name)) {
492 struct tokenval tokval;
493 expr *e;
494 char *p = special;
496 while (*p && !isspace(*p))
497 p++;
498 while (*p && isspace(*p))
499 p++;
500 stdscan_reset();
501 stdscan_bufptr = p;
502 tokval.t_type = TOKEN_INVALID;
503 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
504 if (e) {
505 if (!is_simple(e))
506 error(ERR_NONFATAL, "cannot use relocatable"
507 " expression as symbol size");
508 else
509 (*s)->size = reloc_value(e);
513 * Remove it from the list of unresolved sizes.
515 nasm_free((*s)->name);
516 *s = (*s)->nextfwd;
517 return;
519 return; /* it wasn't an important one */
522 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
523 strslen += 1 + strlen(name);
525 lastsym = sym = saa_wstruct(syms);
527 sym->strpos = pos;
528 sym->type = is_global ? SYM_GLOBAL : 0;
529 sym->other = STV_DEFAULT;
530 sym->size = 0;
531 if (segment == NO_SEG)
532 sym->section = SHN_ABS;
533 else {
534 int i;
535 sym->section = SHN_UNDEF;
536 if (nsects == 0 && segment == def_seg) {
537 int tempint;
538 if (segment != elf_section_names(".text", 2, &tempint))
539 error(ERR_PANIC,
540 "strange segment conditions in ELF driver");
541 sym->section = nsects;
542 } else {
543 for (i = 0; i < nsects; i++)
544 if (segment == sects[i]->index) {
545 sym->section = i + 1;
546 break;
551 if (is_global == 2) {
552 sym->size = offset;
553 sym->value = 0;
554 sym->section = SHN_COMMON;
556 * We have a common variable. Check the special text to see
557 * if it's a valid number and power of two; if so, store it
558 * as the alignment for the common variable.
560 if (special) {
561 bool err;
562 sym->value = readnum(special, &err);
563 if (err)
564 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
565 " valid number", special);
566 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
567 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
568 " power of two", special);
570 special_used = true;
571 } else
572 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
574 if (sym->type == SYM_GLOBAL) {
576 * If sym->section == SHN_ABS, then the first line of the
577 * else section would cause a core dump, because its a reference
578 * beyond the end of the section array.
579 * This behaviour is exhibited by this code:
580 * GLOBAL crash_nasm
581 * crash_nasm equ 0
582 * To avoid such a crash, such requests are silently discarded.
583 * This may not be the best solution.
585 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
586 bsym = raa_write(bsym, segment, nglobs);
587 } else if (sym->section != SHN_ABS) {
589 * This is a global symbol; so we must add it to the linked
590 * list of global symbols in its section. We'll push it on
591 * the beginning of the list, because it doesn't matter
592 * much which end we put it on and it's easier like this.
594 * In addition, we check the special text for symbol
595 * type and size information.
597 sym->next = sects[sym->section - 1]->gsyms;
598 sects[sym->section - 1]->gsyms = sym;
600 if (special) {
601 int n = strcspn(special, " \t");
603 if (!nasm_strnicmp(special, "function", n))
604 sym->type |= STT_FUNC;
605 else if (!nasm_strnicmp(special, "data", n) ||
606 !nasm_strnicmp(special, "object", n))
607 sym->type |= STT_OBJECT;
608 else if (!nasm_strnicmp(special, "notype", n))
609 sym->type |= STT_NOTYPE;
610 else
611 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
612 n, special);
613 special += n;
615 while (isspace(*special))
616 ++special;
617 if (*special) {
618 n = strcspn(special, " \t");
619 if (!nasm_strnicmp(special, "default", n))
620 sym->other = STV_DEFAULT;
621 else if (!nasm_strnicmp(special, "internal", n))
622 sym->other = STV_INTERNAL;
623 else if (!nasm_strnicmp(special, "hidden", n))
624 sym->other = STV_HIDDEN;
625 else if (!nasm_strnicmp(special, "protected", n))
626 sym->other = STV_PROTECTED;
627 else
628 n = 0;
629 special += n;
632 if (*special) {
633 struct tokenval tokval;
634 expr *e;
635 int fwd = 0;
636 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
638 while (special[n] && isspace(special[n]))
639 n++;
641 * We have a size expression; attempt to
642 * evaluate it.
644 stdscan_reset();
645 stdscan_bufptr = special + n;
646 tokval.t_type = TOKEN_INVALID;
647 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
648 NULL);
649 if (fwd) {
650 sym->nextfwd = fwds;
651 fwds = sym;
652 sym->name = nasm_strdup(name);
653 } else if (e) {
654 if (!is_simple(e))
655 error(ERR_NONFATAL, "cannot use relocatable"
656 " expression as symbol size");
657 else
658 sym->size = reloc_value(e);
660 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
662 special_used = true;
665 sym->globnum = nglobs;
666 nglobs++;
667 } else
668 nlocals++;
670 if (special && !special_used)
671 error(ERR_NONFATAL, "no special symbol features supported here");
674 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
676 struct Reloc *r;
678 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
679 sect->tail = &r->next;
680 r->next = NULL;
682 r->address = sect->len;
683 if (segment == NO_SEG)
684 r->symbol = 0;
685 else {
686 int i;
687 r->symbol = 0;
688 for (i = 0; i < nsects; i++)
689 if (segment == sects[i]->index)
690 r->symbol = i + 2;
691 if (!r->symbol)
692 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
694 r->type = type;
696 sect->nrelocs++;
700 * This routine deals with ..got and ..sym relocations: the more
701 * complicated kinds. In shared-library writing, some relocations
702 * with respect to global symbols must refer to the precise symbol
703 * rather than referring to an offset from the base of the section
704 * _containing_ the symbol. Such relocations call to this routine,
705 * which searches the symbol list for the symbol in question.
707 * R_386_GOT32 references require the _exact_ symbol address to be
708 * used; R_386_32 references can be at an offset from the symbol.
709 * The boolean argument `exact' tells us this.
711 * Return value is the adjusted value of `addr', having become an
712 * offset from the symbol rather than the section. Should always be
713 * zero when returning from an exact call.
715 * Limitation: if you define two symbols at the same place,
716 * confusion will occur.
718 * Inefficiency: we search, currently, using a linked list which
719 * isn't even necessarily sorted.
721 static int32_t elf_add_gsym_reloc(struct Section *sect,
722 int32_t segment, int64_t offset,
723 int type, bool exact)
725 struct Reloc *r;
726 struct Section *s;
727 struct Symbol *sym, *sm;
728 int i;
731 * First look up the segment/offset pair and find a global
732 * symbol corresponding to it. If it's not one of our segments,
733 * then it must be an external symbol, in which case we're fine
734 * doing a normal elf_add_reloc after first sanity-checking
735 * that the offset from the symbol is zero.
737 s = NULL;
738 for (i = 0; i < nsects; i++)
739 if (segment == sects[i]->index) {
740 s = sects[i];
741 break;
743 if (!s) {
744 if (exact && offset != 0)
745 error(ERR_NONFATAL, "unable to find a suitable global symbol"
746 " for this reference");
747 else
748 elf_add_reloc(sect, segment, type);
749 return offset;
752 if (exact) {
754 * Find a symbol pointing _exactly_ at this one.
756 for (sym = s->gsyms; sym; sym = sym->next)
757 if (sym->value == offset)
758 break;
759 } else {
761 * Find the nearest symbol below this one.
763 sym = NULL;
764 for (sm = s->gsyms; sm; sm = sm->next)
765 if (sm->value <= offset && (!sym || sm->value > sym->value))
766 sym = sm;
768 if (!sym && exact) {
769 error(ERR_NONFATAL, "unable to find a suitable global symbol"
770 " for this reference");
771 return 0;
774 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
775 sect->tail = &r->next;
776 r->next = NULL;
778 r->address = sect->len;
779 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
780 r->type = type;
782 sect->nrelocs++;
784 return offset - sym->value;
787 static void elf_out(int32_t segto, const void *data, uint32_t type,
788 int32_t segment, int32_t wrt)
790 struct Section *s;
791 int32_t realbytes = type & OUT_SIZMASK;
792 int64_t addr;
793 uint8_t mydata[16], *p;
794 int i;
795 static struct symlininfo sinfo;
797 type &= OUT_TYPMASK;
799 #if defined(DEBUG) && DEBUG>2
800 fprintf(stderr,
801 " elf_out type: %x seg: %d bytes: %x data: %x\n",
802 (type >> 24), segment, realbytes, *(int32_t *)data);
803 #endif
806 * handle absolute-assembly (structure definitions)
808 if (segto == NO_SEG) {
809 if (type != OUT_RESERVE)
810 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
811 " space");
812 return;
815 s = NULL;
816 for (i = 0; i < nsects; i++)
817 if (segto == sects[i]->index) {
818 s = sects[i];
819 break;
821 if (!s) {
822 int tempint; /* ignored */
823 if (segto != elf_section_names(".text", 2, &tempint))
824 error(ERR_PANIC, "strange segment conditions in ELF driver");
825 else {
826 s = sects[nsects - 1];
827 i = nsects - 1;
831 /* again some stabs debugging stuff */
832 if (of_elf64.current_dfmt) {
833 sinfo.offset = s->len;
834 sinfo.section = i;
835 sinfo.name = s->name;
836 of_elf64.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
838 /* end of debugging stuff */
840 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
841 error(ERR_WARNING, "attempt to initialize memory in"
842 " BSS section `%s': ignored", s->name);
843 if (type == OUT_REL2ADR)
844 realbytes = 2;
845 else if (type == OUT_REL4ADR)
846 realbytes = 4;
847 s->len += realbytes;
848 return;
851 if (type == OUT_RESERVE) {
852 if (s->type == SHT_PROGBITS) {
853 error(ERR_WARNING, "uninitialized space declared in"
854 " non-BSS section `%s': zeroing", s->name);
855 elf_sect_write(s, NULL, realbytes);
856 } else
857 s->len += realbytes;
858 } else if (type == OUT_RAWDATA) {
859 if (segment != NO_SEG)
860 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
861 elf_sect_write(s, data, realbytes);
862 } else if (type == OUT_ADDRESS) {
863 bool gnu16 = false;
864 addr = *(int64_t *)data;
865 if (segment != NO_SEG) {
866 if (segment % 2) {
867 error(ERR_NONFATAL, "ELF format does not support"
868 " segment base references");
869 } else {
870 if (wrt == NO_SEG) {
871 switch (realbytes) {
872 case 2:
873 elf_add_reloc(s, segment, R_X86_64_16);
874 break;
875 case 4:
876 elf_add_reloc(s, segment, R_X86_64_32);
877 break;
878 case 8:
879 elf_add_reloc(s, segment, R_X86_64_64);
880 break;
881 default:
882 error(ERR_PANIC, "internal error elf64-hpa-871");
883 break;
885 } else if (wrt == elf_gotpc_sect + 1) {
887 * The user will supply GOT relative to $$. ELF
888 * will let us have GOT relative to $. So we
889 * need to fix up the data item by $-$$.
891 addr += s->len;
892 elf_add_reloc(s, segment, R_X86_64_GOTPCREL);
893 } else if (wrt == elf_gotoff_sect + 1) {
894 elf_add_reloc(s, segment, R_X86_64_GOTTPOFF);
895 } else if (wrt == elf_got_sect + 1) {
896 addr = elf_add_gsym_reloc(s, segment, addr,
897 R_X86_64_GOT32, true);
898 } else if (wrt == elf_sym_sect + 1) {
899 switch (realbytes) {
900 case 2:
901 gnu16 = true;
902 addr = elf_add_gsym_reloc(s, segment, addr,
903 R_X86_64_16, false);
904 break;
905 case 4:
906 addr = elf_add_gsym_reloc(s, segment, addr,
907 R_X86_64_32, false);
908 break;
909 case 8:
910 addr = elf_add_gsym_reloc(s, segment, addr,
911 R_X86_64_64, false);
912 break;
913 default:
914 error(ERR_PANIC, "internal error elf64-hpa-903");
915 break;
917 } else if (wrt == elf_plt_sect + 1) {
918 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
919 "relative PLT references");
920 } else {
921 error(ERR_NONFATAL, "ELF format does not support this"
922 " use of WRT");
923 wrt = NO_SEG; /* we can at least _try_ to continue */
927 p = mydata;
928 if (gnu16) {
929 WRITESHORT(p, addr);
930 } else {
931 if (realbytes != 8 && realbytes != 4 && segment != NO_SEG) {
932 error(ERR_NONFATAL,
933 "Unsupported non-64-bit ELF relocation");
935 if (realbytes == 4) WRITELONG(p, addr);
936 else WRITEDLONG(p, (int64_t)addr);
938 elf_sect_write(s, mydata, realbytes);
939 } else if (type == OUT_REL2ADR) {
940 if (segment == segto)
941 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
942 if (segment != NO_SEG && segment % 2) {
943 error(ERR_NONFATAL, "ELF format does not support"
944 " segment base references");
945 } else {
946 if (wrt == NO_SEG) {
947 elf_add_reloc(s, segment, R_X86_64_PC16);
948 } else {
949 error(ERR_NONFATAL,
950 "Unsupported non-32-bit ELF relocation [2]");
953 p = mydata;
954 WRITESHORT(p, *(int32_t *)data - realbytes);
955 elf_sect_write(s, mydata, 2L);
956 } else if (type == OUT_REL4ADR) {
957 if (segment == segto)
958 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
959 if (segment != NO_SEG && segment % 2) {
960 error(ERR_NONFATAL, "ELF format does not support"
961 " segment base references");
962 } else {
963 if (wrt == NO_SEG) {
964 elf_add_reloc(s, segment, R_X86_64_PC32);
965 } else if (wrt == elf_plt_sect + 1) {
966 elf_add_reloc(s, segment, R_X86_64_PLT32);
967 } else if (wrt == elf_gotpc_sect + 1 ||
968 wrt == elf_gotoff_sect + 1 ||
969 wrt == elf_got_sect + 1) {
970 error(ERR_NONFATAL, "ELF format cannot produce PC-"
971 "relative GOT references");
972 } else {
973 error(ERR_NONFATAL, "ELF format does not support this"
974 " use of WRT");
975 wrt = NO_SEG; /* we can at least _try_ to continue */
978 p = mydata;
979 WRITELONG(p, *(int32_t *)data - realbytes);
980 elf_sect_write(s, mydata, 4L);
984 static void elf_write(void)
986 int nsections, align;
987 int scount;
988 char *p;
989 int commlen;
990 char comment[64];
991 int i;
993 struct SAA *symtab;
994 int32_t symtablen, symtablocal;
997 * Work out how many sections we will have. We have SHN_UNDEF,
998 * then the flexible user sections, then the four fixed
999 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1000 * then optionally relocation sections for the user sections.
1002 if (of_elf64.current_dfmt == &df_stabs)
1003 nsections = 8;
1004 else
1005 nsections = 5; /* SHN_UNDEF and the fixed ones */
1007 add_sectname("", ".comment");
1008 add_sectname("", ".shstrtab");
1009 add_sectname("", ".symtab");
1010 add_sectname("", ".strtab");
1011 for (i = 0; i < nsects; i++) {
1012 nsections++; /* for the section itself */
1013 if (sects[i]->head) {
1014 nsections++; /* for its relocations without addends*/
1015 add_sectname(".rela", sects[i]->name);
1019 if (of_elf64.current_dfmt == &df_stabs) {
1020 /* in case the debug information is wanted, just add these three sections... */
1021 add_sectname("", ".stab");
1022 add_sectname("", ".stabstr");
1023 add_sectname(".rel", ".stab");
1027 * Do the comment.
1029 *comment = '\0';
1030 commlen =
1031 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1034 * Output the ELF header.
1036 fwrite("\177ELF\2\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
1037 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1038 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1039 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1040 fwriteint64_t(0L, elffp); /* no entry point */
1041 fwriteint64_t(0L, elffp); /* no program header table */
1042 fwriteint64_t(0x40L, elffp); /* section headers straight after
1043 * ELF header plus alignment */
1044 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1045 fwriteint16_t(0x40, elffp); /* size of ELF header */
1046 fwriteint16_t(0, elffp); /* no program header table, again */
1047 fwriteint16_t(0, elffp); /* still no program header table */
1048 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1049 fwriteint16_t(nsections, elffp); /* number of sections */
1050 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1051 * section header table */
1054 * Build the symbol table and relocation tables.
1056 symtab = elf_build_symtab(&symtablen, &symtablocal);
1057 for (i = 0; i < nsects; i++)
1058 if (sects[i]->head)
1059 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1060 sects[i]->head);
1063 * Now output the section header table.
1066 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1067 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1068 elf_foffs += align;
1069 elf_nsect = 0;
1070 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1072 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1073 scount = 1; /* needed for the stabs debugging to track the symtable section */
1074 p = shstrtab + 1;
1075 for (i = 0; i < nsects; i++) {
1076 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1077 (sects[i]->type == SHT_PROGBITS ?
1078 sects[i]->data : NULL), true,
1079 sects[i]->len, 0, 0, sects[i]->align, 0);
1080 p += strlen(p) + 1;
1081 scount++; /* dito */
1083 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1084 scount++; /* dito */
1085 p += strlen(p) + 1;
1086 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1087 scount++; /* dito */
1088 p += strlen(p) + 1;
1089 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1090 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1091 p += strlen(p) + 1;
1092 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1093 for (i = 0; i < nsects; i++)
1094 if (sects[i]->head) {
1095 p += strlen(p) + 1;
1096 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, true,
1097 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1099 if (of_elf64.current_dfmt == &df_stabs) {
1100 /* for debugging information, create the last three sections
1101 which are the .stab , .stabstr and .rel.stab sections respectively */
1103 /* this function call creates the stab sections in memory */
1104 stabs64_generate();
1106 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1107 p += strlen(p) + 1;
1108 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1109 nsections - 2, 0, 4, 12);
1111 p += strlen(p) + 1;
1112 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1113 stabstrlen, 0, 0, 4, 0);
1115 p += strlen(p) + 1;
1116 /* link -> symtable info -> section to refer to */
1117 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1118 stabrellen, symtabsection, nsections - 3, 4,
1119 16);
1122 fwrite(align_str, align, 1, elffp);
1125 * Now output the sections.
1127 elf_write_sections();
1129 nasm_free(elf_sects);
1130 saa_free(symtab);
1133 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1135 struct SAA *s = saa_init(1L);
1136 struct Symbol *sym;
1137 uint8_t entry[24], *p;
1138 int i;
1140 *len = *local = 0;
1143 * First, an all-zeros entry, required by the ELF spec.
1145 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1146 *len += 24;
1147 (*local)++;
1150 * Next, an entry for the file name.
1152 p = entry;
1153 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1154 WRITESHORT(p, STT_FILE); /* type FILE */
1155 WRITESHORT(p, SHN_ABS);
1156 WRITEDLONG(p, (uint64_t) 0); /* no value */
1157 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1158 saa_wbytes(s, entry, 24L);
1159 *len += 24;
1160 (*local)++;
1163 * Now some standard symbols defining the segments, for relocation
1164 * purposes.
1166 for (i = 1; i <= nsects; i++) {
1167 p = entry;
1168 WRITELONG(p, 0); /* no symbol name */
1169 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1170 WRITESHORT(p, i); /* section id */
1171 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1172 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1173 saa_wbytes(s, entry, 24L);
1174 *len += 24;
1175 (*local)++;
1179 * Now the other local symbols.
1181 saa_rewind(syms);
1182 while ((sym = saa_rstruct(syms))) {
1183 if (sym->type & SYM_GLOBAL)
1184 continue;
1185 p = entry;
1186 WRITELONG(p, sym->strpos);
1187 WRITECHAR(p, sym->type); /* type and binding */
1188 WRITECHAR(p, sym->other); /* visibility */
1189 WRITESHORT(p, sym->section);
1190 WRITEDLONG(p, (int64_t)sym->value);
1191 WRITEDLONG(p, (int64_t)sym->size);
1192 saa_wbytes(s, entry, 24L);
1193 *len += 24;
1194 (*local)++;
1198 * Now the global symbols.
1200 saa_rewind(syms);
1201 while ((sym = saa_rstruct(syms))) {
1202 if (!(sym->type & SYM_GLOBAL))
1203 continue;
1204 p = entry;
1205 WRITELONG(p, sym->strpos);
1206 WRITECHAR(p, sym->type); /* type and binding */
1207 WRITECHAR(p, sym->other); /* visibility */
1208 WRITESHORT(p, sym->section);
1209 WRITEDLONG(p, (int64_t)sym->value);
1210 WRITEDLONG(p, (int64_t)sym->size);
1211 saa_wbytes(s, entry, 24L);
1212 *len += 24;
1215 return s;
1218 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1220 struct SAA *s;
1221 uint8_t *p, entry[24];
1223 if (!r)
1224 return NULL;
1226 s = saa_init(1L);
1227 *len = 0;
1229 while (r) {
1230 int64_t sym = r->symbol;
1232 if (sym >= GLOBAL_TEMP_BASE)
1233 sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1235 p = entry;
1236 WRITEDLONG(p, r->address);
1237 WRITEDLONG(p, (sym << 32) + r->type);
1238 WRITEDLONG(p, (uint64_t) 0);
1239 saa_wbytes(s, entry, 24L);
1240 *len += 24;
1242 r = r->next;
1245 return s;
1248 static void elf_section_header(int name, int type, int flags,
1249 void *data, bool is_saa, int32_t datalen,
1250 int link, int info, int align, int eltsize)
1252 elf_sects[elf_nsect].data = data;
1253 elf_sects[elf_nsect].len = datalen;
1254 elf_sects[elf_nsect].is_saa = is_saa;
1255 elf_nsect++;
1257 fwriteint32_t((int32_t)name, elffp);
1258 fwriteint32_t((int32_t)type, elffp);
1259 fwriteint64_t((int64_t)flags, elffp);
1260 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1261 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1262 fwriteint64_t(datalen, elffp);
1263 if (data)
1264 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1265 fwriteint32_t((int32_t)link, elffp);
1266 fwriteint32_t((int32_t)info, elffp);
1267 fwriteint64_t((int64_t)align, elffp);
1268 fwriteint64_t((int64_t)eltsize, elffp);
1271 static void elf_write_sections(void)
1273 int i;
1274 for (i = 0; i < elf_nsect; i++)
1275 if (elf_sects[i].data) {
1276 int32_t len = elf_sects[i].len;
1277 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1278 int32_t align = reallen - len;
1279 if (elf_sects[i].is_saa)
1280 saa_fpwrite(elf_sects[i].data, elffp);
1281 else
1282 fwrite(elf_sects[i].data, len, 1, elffp);
1283 fwrite(align_str, align, 1, elffp);
1287 static void elf_sect_write(struct Section *sect,
1288 const uint8_t *data, uint32_t len)
1290 saa_wbytes(sect->data, data, len);
1291 sect->len += len;
1294 static int32_t elf_segbase(int32_t segment)
1296 return segment;
1299 static int elf_directive(char *directive, char *value, int pass)
1301 (void)directive;
1302 (void)value;
1303 (void)pass;
1304 return 0;
1307 static void elf_filename(char *inname, char *outname, efunc error)
1309 strcpy(elf_module, inname);
1310 standard_extension(inname, outname, ".o", error);
1313 static const char *elf_stdmac[] = {
1314 "%define __SECT__ [section .text]",
1315 "%macro __NASM_CDecl__ 1",
1316 "%define $_%1 $%1",
1317 "%endmacro",
1318 NULL
1320 static int elf_set_info(enum geninfo type, char **val)
1322 (void)type;
1323 (void)val;
1324 return 0;
1327 static struct dfmt df_stabs = {
1328 "ELF64 (X86_64) stabs debug format for Linux",
1329 "stabs",
1330 stabs64_init,
1331 stabs64_linenum,
1332 stabs64_deflabel,
1333 stabs64_directive,
1334 stabs64_typevalue,
1335 stabs64_output,
1336 stabs64_cleanup
1339 struct dfmt *elf64_debugs_arr[2] = { &df_stabs, NULL };
1341 struct ofmt of_elf64 = {
1342 "ELF64 (x86_64) object files (e.g. Linux)",
1343 "elf64",
1344 NULL,
1345 elf64_debugs_arr,
1346 &null_debug_form,
1347 elf_stdmac,
1348 elf_init,
1349 elf_set_info,
1350 elf_out,
1351 elf_deflabel,
1352 elf_section_names,
1353 elf_segbase,
1354 elf_directive,
1355 elf_filename,
1356 elf_cleanup
1359 /* again, the stabs debugging stuff (code) */
1361 void stabs64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1363 (void)of;
1364 (void)id;
1365 (void)fp;
1366 (void)error;
1369 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1371 (void)segto;
1373 if (!stabs_filename) {
1374 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1375 strcpy(stabs_filename, filename);
1376 } else {
1377 if (strcmp(stabs_filename, filename)) {
1378 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1379 in fact, this leak comes in quite handy to maintain a list of files
1380 encountered so far in the symbol lines... */
1382 /* why not nasm_free(stabs_filename); we're done with the old one */
1384 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1385 strcpy(stabs_filename, filename);
1388 stabs_immcall = 1;
1389 currentline = linenumber;
1392 void stabs64_deflabel(char *name, int32_t segment, int32_t offset, int is_global,
1393 char *special)
1395 (void)name;
1396 (void)segment;
1397 (void)offset;
1398 (void)is_global;
1399 (void)special;
1402 void stabs64_directive(const char *directive, const char *params)
1404 (void)directive;
1405 (void)params;
1408 void stabs64_typevalue(int32_t type)
1410 int32_t stype, ssize;
1411 switch (TYM_TYPE(type)) {
1412 case TY_LABEL:
1413 ssize = 0;
1414 stype = STT_NOTYPE;
1415 break;
1416 case TY_BYTE:
1417 ssize = 1;
1418 stype = STT_OBJECT;
1419 break;
1420 case TY_WORD:
1421 ssize = 2;
1422 stype = STT_OBJECT;
1423 break;
1424 case TY_DWORD:
1425 ssize = 4;
1426 stype = STT_OBJECT;
1427 break;
1428 case TY_FLOAT:
1429 ssize = 4;
1430 stype = STT_OBJECT;
1431 break;
1432 case TY_QWORD:
1433 ssize = 8;
1434 stype = STT_OBJECT;
1435 break;
1436 case TY_TBYTE:
1437 ssize = 10;
1438 stype = STT_OBJECT;
1439 break;
1440 case TY_OWORD:
1441 ssize = 8;
1442 stype = STT_OBJECT;
1443 break;
1444 case TY_COMMON:
1445 ssize = 0;
1446 stype = STT_COMMON;
1447 break;
1448 case TY_SEG:
1449 ssize = 0;
1450 stype = STT_SECTION;
1451 break;
1452 case TY_EXTERN:
1453 ssize = 0;
1454 stype = STT_NOTYPE;
1455 break;
1456 case TY_EQU:
1457 ssize = 0;
1458 stype = STT_NOTYPE;
1459 break;
1460 default:
1461 ssize = 0;
1462 stype = STT_NOTYPE;
1463 break;
1465 if (stype == STT_OBJECT && !lastsym->type) {
1466 lastsym->size = ssize;
1467 lastsym->type = stype;
1471 void stabs64_output(int type, void *param)
1473 struct symlininfo *s;
1474 struct linelist *el;
1475 if (type == TY_STABSSYMLIN) {
1476 if (stabs_immcall) {
1477 s = (struct symlininfo *)param;
1478 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1479 return; /* we are only interested in the text stuff */
1480 numlinestabs++;
1481 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1482 el->info.offset = s->offset;
1483 el->info.section = s->section;
1484 el->info.name = s->name;
1485 el->line = currentline;
1486 el->filename = stabs_filename;
1487 el->next = 0;
1488 if (stabslines) {
1489 stabslines->last->next = el;
1490 stabslines->last = el;
1491 } else {
1492 stabslines = el;
1493 stabslines->last = el;
1497 stabs_immcall = 0;
1500 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1501 do {\
1502 WRITELONG(p,n_strx); \
1503 WRITECHAR(p,n_type); \
1504 WRITECHAR(p,n_other); \
1505 WRITESHORT(p,n_desc); \
1506 WRITELONG(p,n_value); \
1507 } while (0)
1509 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1511 void stabs64_generate(void)
1513 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1514 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1515 char **allfiles;
1516 int *fileidx;
1518 struct linelist *ptr;
1520 ptr = stabslines;
1522 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1523 for (i = 0; i < numlinestabs; i++)
1524 allfiles[i] = 0;
1525 numfiles = 0;
1526 while (ptr) {
1527 if (numfiles == 0) {
1528 allfiles[0] = ptr->filename;
1529 numfiles++;
1530 } else {
1531 for (i = 0; i < numfiles; i++) {
1532 if (!strcmp(allfiles[i], ptr->filename))
1533 break;
1535 if (i >= numfiles) {
1536 allfiles[i] = ptr->filename;
1537 numfiles++;
1540 ptr = ptr->next;
1542 strsize = 1;
1543 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1544 for (i = 0; i < numfiles; i++) {
1545 fileidx[i] = strsize;
1546 strsize += strlen(allfiles[i]) + 1;
1548 mainfileindex = 0;
1549 for (i = 0; i < numfiles; i++) {
1550 if (!strcmp(allfiles[i], elf_module)) {
1551 mainfileindex = i;
1552 break;
1556 /* worst case size of the stab buffer would be:
1557 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1559 sbuf =
1560 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1561 sizeof(struct stabentry));
1563 ssbuf = (uint8_t *)nasm_malloc(strsize);
1565 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1566 rptr = rbuf;
1568 for (i = 0; i < numfiles; i++) {
1569 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1571 ssbuf[0] = 0;
1573 stabstrlen = strsize; /* set global variable for length of stab strings */
1575 sptr = sbuf;
1576 ptr = stabslines;
1577 numstabs = 0;
1579 if (ptr) {
1580 /* this is the first stab, its strx points to the filename of the
1581 the source-file, the n_desc field should be set to the number
1582 of remaining stabs
1584 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1586 /* this is the stab for the main source file */
1587 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1589 /* relocation table entry */
1591 /* Since the symbol table has two entries before */
1592 /* the section symbols, the index in the info.section */
1593 /* member must be adjusted by adding 2 */
1595 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1596 WRITELONG(rptr, R_X86_64_32);
1597 WRITELONG(rptr, ptr->info.section + 2);
1599 numstabs++;
1600 currfile = mainfileindex;
1603 while (ptr) {
1604 if (strcmp(allfiles[currfile], ptr->filename)) {
1605 /* oops file has changed... */
1606 for (i = 0; i < numfiles; i++)
1607 if (!strcmp(allfiles[i], ptr->filename))
1608 break;
1609 currfile = i;
1610 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1611 ptr->info.offset);
1612 numstabs++;
1614 /* relocation table entry */
1616 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1617 WRITELONG(rptr, R_X86_64_32);
1618 WRITELONG(rptr, ptr->info.section + 2);
1621 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1622 numstabs++;
1624 /* relocation table entry */
1626 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1627 WRITELONG(rptr, R_X86_64_32);
1628 WRITELONG(rptr, ptr->info.section + 2);
1630 ptr = ptr->next;
1634 ((struct stabentry *)sbuf)->n_desc = numstabs;
1636 nasm_free(allfiles);
1637 nasm_free(fileidx);
1639 stablen = (sptr - sbuf);
1640 stabrellen = (rptr - rbuf);
1641 stabrelbuf = rbuf;
1642 stabbuf = sbuf;
1643 stabstrbuf = ssbuf;
1646 void stabs64_cleanup(void)
1648 struct linelist *ptr, *del;
1649 if (!stabslines)
1650 return;
1651 ptr = stabslines;
1652 while (ptr) {
1653 del = ptr;
1654 ptr = ptr->next;
1655 nasm_free(del);
1657 if (stabbuf)
1658 nasm_free(stabbuf);
1659 if (stabrelbuf)
1660 nasm_free(stabrelbuf);
1661 if (stabstrbuf)
1662 nasm_free(stabstrbuf);
1665 #endif /* OF_ELF */