Cast 64-bit switch expressions to (int) to keep OpenWatcom happy
[nasm.git] / output / outelf64.c
blobf127348085711ae8c078e86d71064543b61c0f92
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 uint64_t len, size;
99 uint32_t nrelocs;
100 int32_t index;
101 uint32_t type; /* SHT_PROGBITS or SHT_NOBITS */
102 uint64_t align; /* alignment: power of two */
103 uint64_t flags; /* section flags */
104 char *name;
105 struct SAA *rel;
106 uint64_t rellen;
107 struct Reloc *head, **tail;
108 struct Symbol *gsyms; /* global symbols in section */
111 #define SECT_DELTA 32
112 static struct Section **sects;
113 static int nsects, sectlen;
115 #define SHSTR_DELTA 256
116 static char *shstrtab;
117 static int shstrtablen, shstrtabsize;
119 static struct SAA *syms;
120 static uint32_t nlocals, nglobs;
122 static int32_t def_seg;
124 static struct RAA *bsym;
126 static struct SAA *strs;
127 static uint32_t strslen;
129 static FILE *elffp;
130 static efunc error;
131 static evalfunc evaluate;
133 static struct Symbol *fwds;
135 static char elf_module[FILENAME_MAX];
137 extern struct ofmt of_elf64;
139 #define SHN_UNDEF 0
141 #define SYM_GLOBAL 0x10
143 #define STV_DEFAULT 0
144 #define STV_INTERNAL 1
145 #define STV_HIDDEN 2
146 #define STV_PROTECTED 3
148 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
150 #define SEG_ALIGN 16 /* alignment of sections in file */
151 #define SEG_ALIGN_1 (SEG_ALIGN-1)
153 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
155 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
156 static struct ELF_SECTDATA {
157 void *data;
158 int64_t len;
159 bool is_saa;
160 } *elf_sects;
161 static int elf_nsect;
162 static int64_t elf_foffs;
164 static void elf_write(void);
165 static void elf_sect_write(struct Section *, const uint8_t *,
166 uint64_t);
167 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
168 int, int);
169 static void elf_write_sections(void);
170 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
171 static struct SAA *elf_build_reltab(uint64_t *, struct Reloc *);
172 static void add_sectname(char *, char *);
174 /* this stuff is needed for the stabs debugging format */
175 #define N_SO 0x64 /* ID for main source file */
176 #define N_SOL 0x84 /* ID for sub-source file */
177 #define N_BINCL 0x82
178 #define N_EINCL 0xA2
179 #define N_SLINE 0x44
180 #define TY_STABSSYMLIN 0x40 /* ouch */
182 struct stabentry {
183 uint32_t n_strx;
184 uint8_t n_type;
185 uint8_t n_other;
186 uint16_t n_desc;
187 uint32_t n_value;
190 struct erel {
191 int offset, info;
194 struct symlininfo {
195 int offset;
196 int section; /* section index */
197 char *name; /* shallow-copied pointer of section name */
200 struct linelist {
201 struct symlininfo info;
202 int line;
203 char *filename;
204 struct linelist *next;
205 struct linelist *last;
208 static struct linelist *stabslines = 0;
209 static int stabs_immcall = 0;
210 static int currentline = 0;
211 static int numlinestabs = 0;
212 static char *stabs_filename = 0;
213 static int symtabsection;
214 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
215 static int stablen, stabstrlen, stabrellen;
217 static struct dfmt df_stabs;
218 static struct Symbol *lastsym;
220 void stabs64_init(struct ofmt *, void *, FILE *, efunc);
221 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
222 void stabs64_deflabel(char *, int32_t, int64_t, int, char *);
223 void stabs64_directive(const char *, const char *);
224 void stabs64_typevalue(int32_t);
225 void stabs64_output(int, void *);
226 void stabs64_generate(void);
227 void stabs64_cleanup(void);
229 /* end of stabs debugging stuff */
232 * Special section numbers which are used to define ELF special
233 * symbols, which can be used with WRT to provide PIC relocation
234 * types.
236 static int32_t elf_gotpc_sect, elf_gotoff_sect;
237 static int32_t elf_got_sect, elf_plt_sect;
238 static int32_t elf_sym_sect;
240 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
242 maxbits = 64;
243 elffp = fp;
244 error = errfunc;
245 evaluate = eval;
246 (void)ldef; /* placate optimisers */
247 sects = NULL;
248 nsects = sectlen = 0;
249 syms = saa_init((int32_t)sizeof(struct Symbol));
250 nlocals = nglobs = 0;
251 bsym = raa_init();
252 strs = saa_init(1L);
253 saa_wbytes(strs, "\0", 1L);
254 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
255 strslen = 2 + strlen(elf_module);
256 shstrtab = NULL;
257 shstrtablen = shstrtabsize = 0;;
258 add_sectname("", "");
260 fwds = NULL;
262 elf_gotpc_sect = seg_alloc();
263 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf64,
264 error);
265 elf_gotoff_sect = seg_alloc();
266 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf64,
267 error);
268 elf_got_sect = seg_alloc();
269 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf64,
270 error);
271 elf_plt_sect = seg_alloc();
272 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf64,
273 error);
274 elf_sym_sect = seg_alloc();
275 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf64,
276 error);
278 def_seg = seg_alloc();
281 static void elf_cleanup(int debuginfo)
283 struct Reloc *r;
284 int i;
286 (void)debuginfo;
288 elf_write();
289 fclose(elffp);
290 for (i = 0; i < nsects; i++) {
291 if (sects[i]->type != SHT_NOBITS)
292 saa_free(sects[i]->data);
293 if (sects[i]->head)
294 saa_free(sects[i]->rel);
295 while (sects[i]->head) {
296 r = sects[i]->head;
297 sects[i]->head = sects[i]->head->next;
298 nasm_free(r);
301 nasm_free(sects);
302 saa_free(syms);
303 raa_free(bsym);
304 saa_free(strs);
305 if (of_elf64.current_dfmt) {
306 of_elf64.current_dfmt->cleanup();
310 static void add_sectname(char *firsthalf, char *secondhalf)
312 int len = strlen(firsthalf) + strlen(secondhalf);
313 while (shstrtablen + len + 1 > shstrtabsize)
314 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
315 strcpy(shstrtab + shstrtablen, firsthalf);
316 strcat(shstrtab + shstrtablen, secondhalf);
317 shstrtablen += len + 1;
320 static int elf_make_section(char *name, int type, int flags, int align)
322 struct Section *s;
324 s = nasm_malloc(sizeof(*s));
326 if (type != SHT_NOBITS)
327 s->data = saa_init(1L);
328 s->head = NULL;
329 s->tail = &s->head;
330 s->len = s->size = 0;
331 s->nrelocs = 0;
332 if (!strcmp(name, ".text"))
333 s->index = def_seg;
334 else
335 s->index = seg_alloc();
336 add_sectname("", name);
337 s->name = nasm_malloc(1 + strlen(name));
338 strcpy(s->name, name);
339 s->type = type;
340 s->flags = flags;
341 s->align = align;
342 s->gsyms = NULL;
344 if (nsects >= sectlen)
345 sects =
346 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
347 sects[nsects++] = s;
349 return nsects - 1;
352 static int32_t elf_section_names(char *name, int pass, int *bits)
354 char *p;
355 unsigned flags_and, flags_or;
356 uint64_t type, align;
357 int i;
360 * Default is 64 bits.
362 if (!name) {
363 *bits = 64;
364 return def_seg;
367 p = name;
368 while (*p && !isspace(*p))
369 p++;
370 if (*p)
371 *p++ = '\0';
372 flags_and = flags_or = type = align = 0;
374 while (*p && isspace(*p))
375 p++;
376 while (*p) {
377 char *q = p;
378 while (*p && !isspace(*p))
379 p++;
380 if (*p)
381 *p++ = '\0';
382 while (*p && isspace(*p))
383 p++;
385 if (!nasm_strnicmp(q, "align=", 6)) {
386 align = atoi(q + 6);
387 if (align == 0)
388 align = 1;
389 if ((align - 1) & align) { /* means it's not a power of two */
390 error(ERR_NONFATAL, "section alignment %d is not"
391 " a power of two", align);
392 align = 1;
394 } else if (!nasm_stricmp(q, "alloc")) {
395 flags_and |= SHF_ALLOC;
396 flags_or |= SHF_ALLOC;
397 } else if (!nasm_stricmp(q, "noalloc")) {
398 flags_and |= SHF_ALLOC;
399 flags_or &= ~SHF_ALLOC;
400 } else if (!nasm_stricmp(q, "exec")) {
401 flags_and |= SHF_EXECINSTR;
402 flags_or |= SHF_EXECINSTR;
403 } else if (!nasm_stricmp(q, "noexec")) {
404 flags_and |= SHF_EXECINSTR;
405 flags_or &= ~SHF_EXECINSTR;
406 } else if (!nasm_stricmp(q, "write")) {
407 flags_and |= SHF_WRITE;
408 flags_or |= SHF_WRITE;
409 } else if (!nasm_stricmp(q, "nowrite")) {
410 flags_and |= SHF_WRITE;
411 flags_or &= ~SHF_WRITE;
412 } else if (!nasm_stricmp(q, "progbits")) {
413 type = SHT_PROGBITS;
414 } else if (!nasm_stricmp(q, "nobits")) {
415 type = SHT_NOBITS;
419 if (!strcmp(name, ".comment") ||
420 !strcmp(name, ".shstrtab") ||
421 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
422 error(ERR_NONFATAL, "attempt to redefine reserved section"
423 "name `%s'", name);
424 return NO_SEG;
427 for (i = 0; i < nsects; i++)
428 if (!strcmp(name, sects[i]->name))
429 break;
430 if (i == nsects) {
431 if (!strcmp(name, ".text"))
432 i = elf_make_section(name, SHT_PROGBITS,
433 SHF_ALLOC | SHF_EXECINSTR, 16);
434 else if (!strcmp(name, ".rodata"))
435 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
436 else if (!strcmp(name, ".data"))
437 i = elf_make_section(name, SHT_PROGBITS,
438 SHF_ALLOC | SHF_WRITE, 4);
439 else if (!strcmp(name, ".bss"))
440 i = elf_make_section(name, SHT_NOBITS,
441 SHF_ALLOC | SHF_WRITE, 4);
442 else
443 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
444 if (type)
445 sects[i]->type = type;
446 if (align)
447 sects[i]->align = align;
448 sects[i]->flags &= ~flags_and;
449 sects[i]->flags |= flags_or;
450 } else if (pass == 1) {
451 if ((type && sects[i]->type != type)
452 || (align && sects[i]->align != align)
453 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
454 error(ERR_WARNING, "incompatible section attributes ignored on"
455 " redeclaration of section `%s'", name);
458 return sects[i]->index;
461 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
462 int is_global, char *special)
464 int pos = strslen;
465 struct Symbol *sym;
466 bool special_used = false;
468 #if defined(DEBUG) && DEBUG>2
469 fprintf(stderr,
470 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
471 name, segment, offset, is_global, special);
472 #endif
473 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
475 * This is a NASM special symbol. We never allow it into
476 * the ELF symbol table, even if it's a valid one. If it
477 * _isn't_ a valid one, we should barf immediately.
479 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
480 strcmp(name, "..got") && strcmp(name, "..plt") &&
481 strcmp(name, "..sym"))
482 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
483 return;
486 if (is_global == 3) {
487 struct Symbol **s;
489 * Fix up a forward-reference symbol size from the first
490 * pass.
492 for (s = &fwds; *s; s = &(*s)->nextfwd)
493 if (!strcmp((*s)->name, name)) {
494 struct tokenval tokval;
495 expr *e;
496 char *p = special;
498 while (*p && !isspace(*p))
499 p++;
500 while (*p && isspace(*p))
501 p++;
502 stdscan_reset();
503 stdscan_bufptr = p;
504 tokval.t_type = TOKEN_INVALID;
505 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
506 if (e) {
507 if (!is_simple(e))
508 error(ERR_NONFATAL, "cannot use relocatable"
509 " expression as symbol size");
510 else
511 (*s)->size = reloc_value(e);
515 * Remove it from the list of unresolved sizes.
517 nasm_free((*s)->name);
518 *s = (*s)->nextfwd;
519 return;
521 return; /* it wasn't an important one */
524 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
525 strslen += 1 + strlen(name);
527 lastsym = sym = saa_wstruct(syms);
529 sym->strpos = pos;
530 sym->type = is_global ? SYM_GLOBAL : 0;
531 sym->other = STV_DEFAULT;
532 sym->size = 0;
533 if (segment == NO_SEG)
534 sym->section = SHN_ABS;
535 else {
536 int i;
537 sym->section = SHN_UNDEF;
538 if (nsects == 0 && segment == def_seg) {
539 int tempint;
540 if (segment != elf_section_names(".text", 2, &tempint))
541 error(ERR_PANIC,
542 "strange segment conditions in ELF driver");
543 sym->section = nsects;
544 } else {
545 for (i = 0; i < nsects; i++)
546 if (segment == sects[i]->index) {
547 sym->section = i + 1;
548 break;
553 if (is_global == 2) {
554 sym->size = offset;
555 sym->value = 0;
556 sym->section = SHN_COMMON;
558 * We have a common variable. Check the special text to see
559 * if it's a valid number and power of two; if so, store it
560 * as the alignment for the common variable.
562 if (special) {
563 bool err;
564 sym->value = readnum(special, &err);
565 if (err)
566 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
567 " valid number", special);
568 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
569 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
570 " power of two", special);
572 special_used = true;
573 } else
574 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
576 if (sym->type == SYM_GLOBAL) {
578 * If sym->section == SHN_ABS, then the first line of the
579 * else section would cause a core dump, because its a reference
580 * beyond the end of the section array.
581 * This behaviour is exhibited by this code:
582 * GLOBAL crash_nasm
583 * crash_nasm equ 0
584 * To avoid such a crash, such requests are silently discarded.
585 * This may not be the best solution.
587 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
588 bsym = raa_write(bsym, segment, nglobs);
589 } else if (sym->section != SHN_ABS) {
591 * This is a global symbol; so we must add it to the linked
592 * list of global symbols in its section. We'll push it on
593 * the beginning of the list, because it doesn't matter
594 * much which end we put it on and it's easier like this.
596 * In addition, we check the special text for symbol
597 * type and size information.
599 sym->next = sects[sym->section - 1]->gsyms;
600 sects[sym->section - 1]->gsyms = sym;
602 if (special) {
603 int n = strcspn(special, " \t");
605 if (!nasm_strnicmp(special, "function", n))
606 sym->type |= STT_FUNC;
607 else if (!nasm_strnicmp(special, "data", n) ||
608 !nasm_strnicmp(special, "object", n))
609 sym->type |= STT_OBJECT;
610 else if (!nasm_strnicmp(special, "notype", n))
611 sym->type |= STT_NOTYPE;
612 else
613 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
614 n, special);
615 special += n;
617 while (isspace(*special))
618 ++special;
619 if (*special) {
620 n = strcspn(special, " \t");
621 if (!nasm_strnicmp(special, "default", n))
622 sym->other = STV_DEFAULT;
623 else if (!nasm_strnicmp(special, "internal", n))
624 sym->other = STV_INTERNAL;
625 else if (!nasm_strnicmp(special, "hidden", n))
626 sym->other = STV_HIDDEN;
627 else if (!nasm_strnicmp(special, "protected", n))
628 sym->other = STV_PROTECTED;
629 else
630 n = 0;
631 special += n;
634 if (*special) {
635 struct tokenval tokval;
636 expr *e;
637 int fwd = 0;
638 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
640 while (special[n] && isspace(special[n]))
641 n++;
643 * We have a size expression; attempt to
644 * evaluate it.
646 stdscan_reset();
647 stdscan_bufptr = special + n;
648 tokval.t_type = TOKEN_INVALID;
649 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
650 NULL);
651 if (fwd) {
652 sym->nextfwd = fwds;
653 fwds = sym;
654 sym->name = nasm_strdup(name);
655 } else if (e) {
656 if (!is_simple(e))
657 error(ERR_NONFATAL, "cannot use relocatable"
658 " expression as symbol size");
659 else
660 sym->size = reloc_value(e);
662 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
664 special_used = true;
667 sym->globnum = nglobs;
668 nglobs++;
669 } else
670 nlocals++;
672 if (special && !special_used)
673 error(ERR_NONFATAL, "no special symbol features supported here");
676 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
678 struct Reloc *r;
680 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
681 sect->tail = &r->next;
682 r->next = NULL;
684 r->address = sect->len;
685 if (segment == NO_SEG)
686 r->symbol = 0;
687 else {
688 int i;
689 r->symbol = 0;
690 for (i = 0; i < nsects; i++)
691 if (segment == sects[i]->index)
692 r->symbol = i + 2;
693 if (!r->symbol)
694 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
696 r->type = type;
698 sect->nrelocs++;
702 * This routine deals with ..got and ..sym relocations: the more
703 * complicated kinds. In shared-library writing, some relocations
704 * with respect to global symbols must refer to the precise symbol
705 * rather than referring to an offset from the base of the section
706 * _containing_ the symbol. Such relocations call to this routine,
707 * which searches the symbol list for the symbol in question.
709 * R_386_GOT32 references require the _exact_ symbol address to be
710 * used; R_386_32 references can be at an offset from the symbol.
711 * The boolean argument `exact' tells us this.
713 * Return value is the adjusted value of `addr', having become an
714 * offset from the symbol rather than the section. Should always be
715 * zero when returning from an exact call.
717 * Limitation: if you define two symbols at the same place,
718 * confusion will occur.
720 * Inefficiency: we search, currently, using a linked list which
721 * isn't even necessarily sorted.
723 static int32_t elf_add_gsym_reloc(struct Section *sect,
724 int32_t segment, int64_t offset,
725 int type, bool exact)
727 struct Reloc *r;
728 struct Section *s;
729 struct Symbol *sym, *sm;
730 int i;
733 * First look up the segment/offset pair and find a global
734 * symbol corresponding to it. If it's not one of our segments,
735 * then it must be an external symbol, in which case we're fine
736 * doing a normal elf_add_reloc after first sanity-checking
737 * that the offset from the symbol is zero.
739 s = NULL;
740 for (i = 0; i < nsects; i++)
741 if (segment == sects[i]->index) {
742 s = sects[i];
743 break;
745 if (!s) {
746 if (exact && offset != 0)
747 error(ERR_NONFATAL, "unable to find a suitable global symbol"
748 " for this reference");
749 else
750 elf_add_reloc(sect, segment, type);
751 return offset;
754 if (exact) {
756 * Find a symbol pointing _exactly_ at this one.
758 for (sym = s->gsyms; sym; sym = sym->next)
759 if (sym->value == offset)
760 break;
761 } else {
763 * Find the nearest symbol below this one.
765 sym = NULL;
766 for (sm = s->gsyms; sm; sm = sm->next)
767 if (sm->value <= offset && (!sym || sm->value > sym->value))
768 sym = sm;
770 if (!sym && exact) {
771 error(ERR_NONFATAL, "unable to find a suitable global symbol"
772 " for this reference");
773 return 0;
776 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
777 sect->tail = &r->next;
778 r->next = NULL;
780 r->address = sect->len;
781 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
782 r->type = type;
784 sect->nrelocs++;
786 return offset - sym->value;
789 static void elf_out(int32_t segto, const void *data,
790 enum out_type type, uint64_t size,
791 int32_t segment, int32_t wrt)
793 struct Section *s;
794 int64_t addr;
795 uint8_t mydata[16], *p;
796 int i;
797 static struct symlininfo sinfo;
799 #if defined(DEBUG) && DEBUG>2
800 fprintf(stderr,
801 " elf_out type: %x seg: %d bytes: %x data: %"PRIx64"\n",
802 (type >> 24), segment, size, *(int64_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 size = 2;
845 else if (type == OUT_REL4ADR)
846 size = 4;
847 s->len += size;
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, size);
856 } else
857 s->len += size;
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, size);
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 ((int)size) {
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 ((int)size) {
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 (size != 8 && size != 4 && segment != NO_SEG) {
932 error(ERR_NONFATAL,
933 "Unsupported non-64-bit ELF relocation");
935 if (size == 4) WRITELONG(p, addr);
936 else WRITEDLONG(p, (int64_t)addr);
938 elf_sect_write(s, mydata, size);
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, *(int64_t *)data - size);
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, *(int64_t *)data - size);
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(uint64_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, uint64_t flags,
1249 void *data, bool is_saa, uint64_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, uint64_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, int64_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 = 16;
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 */