out: Elf32, Elfx32, Elf64 -- Unify elf_init and elf_deflabel
[nasm.git] / output / outelfx32.c
blobed1a99e0816b06740146cf89f2b58e96f72aa039
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outelfx32.c output routines for the Netwide Assembler to produce
36 * ELF32 (x86_64) object file format
39 #include "compiler.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <ctype.h>
46 #include "nasm.h"
47 #include "nasmlib.h"
48 #include "saa.h"
49 #include "raa.h"
50 #include "stdscan.h"
51 #include "eval.h"
52 #include "output/outform.h"
53 #include "output/outlib.h"
54 #include "rbtree.h"
55 #include "ver.h"
57 #include "output/dwarf.h"
58 #include "output/stabs.h"
59 #include "output/outelf.h"
61 #ifdef OF_ELFX32
63 #define SECT_DELTA 32
64 static struct elf_section **sects;
65 static int nsects, sectlen;
67 #define SHSTR_DELTA 256
68 static char *shstrtab;
69 static int shstrtablen, shstrtabsize;
71 static struct SAA *syms;
72 static uint32_t nlocals, nglobs, ndebugs; /* Symbol counts */
74 static int32_t def_seg;
76 static struct RAA *bsym;
78 static struct SAA *strs;
79 static uint32_t strslen;
81 static struct elf_symbol *fwds;
83 static char elf_module[FILENAME_MAX];
85 extern const struct ofmt of_elfx32;
87 static struct ELF_SECTDATA {
88 void *data;
89 int64_t len;
90 bool is_saa;
91 } *elf_sects;
92 static int elf_nsect, nsections;
93 static int64_t elf_foffs;
95 static void elf_write(void);
96 static void elf_sect_write(struct elf_section *, const void *, size_t);
97 static void elf_sect_writeaddr(struct elf_section *, int64_t, size_t);
98 static void elf_section_header(int, int, uint64_t, void *, bool, uint64_t, int, int,
99 int, int);
100 static void elf_write_sections(void);
101 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
102 static struct SAA *elf_build_reltab(uint64_t *, struct elf_reloc *);
103 static void add_sectname(char *, char *);
105 struct erel {
106 int offset, info;
109 struct symlininfo {
110 int offset;
111 int section; /* index into sects[] */
112 int segto; /* internal section number */
113 char *name; /* shallow-copied pointer of section name */
116 struct linelist {
117 struct linelist *next;
118 struct linelist *last;
119 struct symlininfo info;
120 char *filename;
121 int line;
124 struct sectlist {
125 struct SAA *psaa;
126 int section;
127 int line;
128 int offset;
129 int file;
130 struct sectlist *next;
131 struct sectlist *last;
134 /* common debug variables */
135 static int currentline = 1;
136 static int debug_immcall = 0;
138 /* stabs debug variables */
139 static struct linelist *stabslines = 0;
140 static int numlinestabs = 0;
141 static char *stabs_filename = 0;
142 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
143 static int stablen, stabstrlen, stabrellen;
145 /* dwarf debug variables */
146 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
147 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
148 static int dwarf_numfiles = 0, dwarf_nsections;
149 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
150 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
151 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
152 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
153 abbrevlen, linelen, linerellen, framelen, loclen;
154 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
157 static const struct dfmt df_dwarf;
158 static const struct dfmt df_stabs;
159 static struct elf_symbol *lastsym;
161 /* common debugging routines */
162 static void debug_typevalue(int32_t);
164 /* stabs debugging routines */
165 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t);
166 static void stabs_output(int, void *);
167 static void stabs_generate(void);
168 static void stabs_cleanup(void);
170 /* dwarf debugging routines */
171 static void dwarf_init(void);
172 static void dwarf_linenum(const char *filename, int32_t linenumber, int32_t);
173 static void dwarf_output(int, void *);
174 static void dwarf_generate(void);
175 static void dwarf_cleanup(void);
176 static void dwarf_findfile(const char *);
177 static void dwarf_findsect(const int);
180 * Special NASM section numbers which are used to define ELF special
181 * symbols.
183 static int32_t elf_gotpc_sect, elf_gotoff_sect;
184 static int32_t elf_got_sect, elf_plt_sect;
185 static int32_t elf_sym_sect, elf_gottpoff_sect, elf_tlsie_sect;
187 static void elf_init(void)
189 sects = NULL;
190 nsects = sectlen = 0;
191 syms = saa_init((int32_t)sizeof(struct elf_symbol));
192 nlocals = nglobs = ndebugs = 0;
193 bsym = raa_init();
194 strs = saa_init(1L);
195 saa_wbytes(strs, "\0", 1L);
196 saa_wbytes(strs, elf_module, strlen(elf_module)+1);
197 strslen = 2 + strlen(elf_module);
198 shstrtab = NULL;
199 shstrtablen = shstrtabsize = 0;;
200 add_sectname("", "");
202 fwds = NULL;
205 * FIXME: tlsie is Elf32 only and
206 * gottpoff is Elfx32|64 only.
209 elf_gotpc_sect = seg_alloc();
210 define_label("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false);
211 elf_gotoff_sect = seg_alloc();
212 define_label("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false);
213 elf_got_sect = seg_alloc();
214 define_label("..got", elf_got_sect + 1, 0L, NULL, false, false);
215 elf_plt_sect = seg_alloc();
216 define_label("..plt", elf_plt_sect + 1, 0L, NULL, false, false);
217 elf_sym_sect = seg_alloc();
218 define_label("..sym", elf_sym_sect + 1, 0L, NULL, false, false);
219 elf_gottpoff_sect = seg_alloc();
220 define_label("..gottpoff", elf_gottpoff_sect + 1, 0L, NULL, false, false);
221 elf_tlsie_sect = seg_alloc();
222 define_label("..tlsie", elf_tlsie_sect + 1, 0L, NULL, false, false);
224 def_seg = seg_alloc();
227 static void elf_cleanup(void)
229 struct elf_reloc *r;
230 int i;
232 elf_write();
233 for (i = 0; i < nsects; i++) {
234 if (sects[i]->type != SHT_NOBITS)
235 saa_free(sects[i]->data);
236 if (sects[i]->head)
237 saa_free(sects[i]->rel);
238 while (sects[i]->head) {
239 r = sects[i]->head;
240 sects[i]->head = sects[i]->head->next;
241 nasm_free(r);
244 nasm_free(sects);
245 saa_free(syms);
246 raa_free(bsym);
247 saa_free(strs);
248 dfmt->cleanup();
251 /* add entry to the elf .shstrtab section */
252 static void add_sectname(char *firsthalf, char *secondhalf)
254 int len = strlen(firsthalf) + strlen(secondhalf);
255 while (shstrtablen + len + 1 > shstrtabsize)
256 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
257 strcpy(shstrtab + shstrtablen, firsthalf);
258 strcat(shstrtab + shstrtablen, secondhalf);
259 shstrtablen += len + 1;
262 static int elf_make_section(char *name, int type, int flags, int align)
264 struct elf_section *s;
266 s = nasm_zalloc(sizeof(*s));
268 if (type != SHT_NOBITS)
269 s->data = saa_init(1L);
270 s->tail = &s->head;
271 if (!strcmp(name, ".text"))
272 s->index = def_seg;
273 else
274 s->index = seg_alloc();
275 add_sectname("", name);
277 s->name = nasm_strdup(name);
278 s->type = type;
279 s->flags = flags;
280 s->align = align;
282 if (nsects >= sectlen)
283 sects = nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
284 sects[nsects++] = s;
286 return nsects - 1;
289 static int32_t elf_section_names(char *name, int pass, int *bits)
291 char *p;
292 uint32_t flags, flags_and, flags_or;
293 uint64_t align;
294 int type, i;
296 if (!name) {
297 *bits = ofmt->maxbits;
298 return def_seg;
301 p = nasm_skip_word(name);
302 if (*p)
303 *p++ = '\0';
304 flags_and = flags_or = type = align = 0;
306 elf_section_attrib(name, p, pass, &flags_and,
307 &flags_or, &align, &type);
309 if (!strcmp(name, ".shstrtab") ||
310 !strcmp(name, ".symtab") ||
311 !strcmp(name, ".strtab")) {
312 nasm_error(ERR_NONFATAL, "attempt to redefine reserved section"
313 "name `%s'", name);
314 return NO_SEG;
317 for (i = 0; i < nsects; i++)
318 if (!strcmp(name, sects[i]->name))
319 break;
320 if (i == nsects) {
321 const struct elf_known_section *ks = elf_known_sections;
323 while (ks->name) {
324 if (!strcmp(name, ks->name))
325 break;
326 ks++;
329 type = type ? type : ks->type;
330 align = align ? align : ks->align;
331 flags = (ks->flags & ~flags_and) | flags_or;
333 i = elf_make_section(name, type, flags, align);
334 } else if (pass == 1) {
335 if ((type && sects[i]->type != type)
336 || (align && sects[i]->align != align)
337 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
338 nasm_error(ERR_WARNING, "incompatible section attributes ignored on"
339 " redeclaration of section `%s'", name);
342 return sects[i]->index;
345 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
346 int is_global, char *special)
348 int pos = strslen;
349 struct elf_symbol *sym;
350 bool special_used = false;
352 #if defined(DEBUG) && DEBUG>2
353 nasm_error(ERR_DEBUG,
354 " elf_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
355 name, segment, offset, is_global, special);
356 #endif
357 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
359 * This is a NASM special symbol. We never allow it into
360 * the ELF symbol table, even if it's a valid one. If it
361 * _isn't_ a valid one, we should barf immediately.
363 * FIXME: tlsie is Elf32 only, and gottpoff is Elfx32|64 only.
365 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
366 strcmp(name, "..got") && strcmp(name, "..plt") &&
367 strcmp(name, "..sym") && strcmp(name, "..gottpoff") &&
368 strcmp(name, "..tlsie"))
369 nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
370 return;
373 if (is_global == 3) {
374 struct elf_symbol **s;
376 * Fix up a forward-reference symbol size from the first
377 * pass.
379 for (s = &fwds; *s; s = &(*s)->nextfwd)
380 if (!strcmp((*s)->name, name)) {
381 struct tokenval tokval;
382 expr *e;
383 char *p = nasm_skip_spaces(nasm_skip_word(special));
385 stdscan_reset();
386 stdscan_set(p);
387 tokval.t_type = TOKEN_INVALID;
388 e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL);
389 if (e) {
390 if (!is_simple(e))
391 nasm_error(ERR_NONFATAL, "cannot use relocatable"
392 " expression as symbol size");
393 else
394 (*s)->size = reloc_value(e);
398 * Remove it from the list of unresolved sizes.
400 nasm_free((*s)->name);
401 *s = (*s)->nextfwd;
402 return;
404 return; /* it wasn't an important one */
407 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
408 strslen += 1 + strlen(name);
410 lastsym = sym = saa_wstruct(syms);
412 memset(&sym->symv, 0, sizeof(struct rbtree));
414 sym->strpos = pos;
415 sym->type = is_global ? SYM_GLOBAL : SYM_LOCAL;
416 sym->other = STV_DEFAULT;
417 sym->size = 0;
418 if (segment == NO_SEG)
419 sym->section = SHN_ABS;
420 else {
421 int i;
422 sym->section = SHN_UNDEF;
423 if (segment == def_seg) {
424 /* we have to be sure at least text section is there */
425 int tempint;
426 if (segment != elf_section_names(".text", 2, &tempint))
427 nasm_panic(0, "strange segment conditions in ELF driver");
429 for (i = 0; i < nsects; i++) {
430 if (segment == sects[i]->index) {
431 sym->section = i + 1;
432 break;
437 if (is_global == 2) {
438 sym->size = offset;
439 sym->symv.key = 0;
440 sym->section = SHN_COMMON;
442 * We have a common variable. Check the special text to see
443 * if it's a valid number and power of two; if so, store it
444 * as the alignment for the common variable.
446 if (special) {
447 bool err;
448 sym->symv.key = readnum(special, &err);
449 if (err)
450 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
451 " valid number", special);
452 else if ((sym->symv.key | (sym->symv.key - 1)) != 2 * sym->symv.key - 1)
453 nasm_error(ERR_NONFATAL, "alignment constraint `%s' is not a"
454 " power of two", special);
456 special_used = true;
457 } else
458 sym->symv.key = (sym->section == SHN_UNDEF ? 0 : offset);
460 if (sym->type == SYM_GLOBAL) {
462 * If sym->section == SHN_ABS, then the first line of the
463 * else section would cause a core dump, because its a reference
464 * beyond the end of the section array.
465 * This behaviour is exhibited by this code:
466 * GLOBAL crash_nasm
467 * crash_nasm equ 0
468 * To avoid such a crash, such requests are silently discarded.
469 * This may not be the best solution.
471 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
472 bsym = raa_write(bsym, segment, nglobs);
473 } else if (sym->section != SHN_ABS) {
475 * This is a global symbol; so we must add it to the rbtree
476 * of global symbols in its section.
478 * In addition, we check the special text for symbol
479 * type and size information.
481 sects[sym->section-1]->gsyms =
482 rb_insert(sects[sym->section-1]->gsyms, &sym->symv);
484 if (special) {
485 int n = strcspn(special, " \t");
487 if (!nasm_strnicmp(special, "function", n))
488 sym->type |= STT_FUNC;
489 else if (!nasm_strnicmp(special, "data", n) ||
490 !nasm_strnicmp(special, "object", n))
491 sym->type |= STT_OBJECT;
492 else if (!nasm_strnicmp(special, "notype", n))
493 sym->type |= STT_NOTYPE;
494 else
495 nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
496 n, special);
497 special += n;
499 special = nasm_skip_spaces(special);
500 if (*special) {
501 n = strcspn(special, " \t");
502 if (!nasm_strnicmp(special, "default", n))
503 sym->other = STV_DEFAULT;
504 else if (!nasm_strnicmp(special, "internal", n))
505 sym->other = STV_INTERNAL;
506 else if (!nasm_strnicmp(special, "hidden", n))
507 sym->other = STV_HIDDEN;
508 else if (!nasm_strnicmp(special, "protected", n))
509 sym->other = STV_PROTECTED;
510 else
511 n = 0;
512 special += n;
515 if (*special) {
516 struct tokenval tokval;
517 expr *e;
518 int fwd = 0;
519 char *saveme = stdscan_get();
521 while (special[n] && nasm_isspace(special[n]))
522 n++;
524 * We have a size expression; attempt to
525 * evaluate it.
527 stdscan_reset();
528 stdscan_set(special + n);
529 tokval.t_type = TOKEN_INVALID;
530 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, NULL);
531 if (fwd) {
532 sym->nextfwd = fwds;
533 fwds = sym;
534 sym->name = nasm_strdup(name);
535 } else if (e) {
536 if (!is_simple(e))
537 nasm_error(ERR_NONFATAL, "cannot use relocatable"
538 " expression as symbol size");
539 else
540 sym->size = reloc_value(e);
542 stdscan_set(saveme);
544 special_used = true;
547 * If TLS segment, mark symbol accordingly.
549 if (sects[sym->section - 1]->flags & SHF_TLS) {
550 sym->type &= 0xf0;
551 sym->type |= STT_TLS;
554 sym->globnum = nglobs;
555 nglobs++;
556 } else
557 nlocals++;
559 if (special && !special_used)
560 nasm_error(ERR_NONFATAL, "no special symbol features supported here");
563 static void elf_add_reloc(struct elf_section *sect, int32_t segment,
564 int64_t offset, int type)
566 struct elf_reloc *r;
568 r = *sect->tail = nasm_zalloc(sizeof(struct elf_reloc));
569 sect->tail = &r->next;
571 r->address = sect->len;
572 r->offset = offset;
574 if (segment != NO_SEG) {
575 int i;
576 for (i = 0; i < nsects; i++)
577 if (segment == sects[i]->index)
578 r->symbol = i + 2;
579 if (!r->symbol)
580 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
582 r->type = type;
584 sect->nrelocs++;
588 * This routine deals with ..got and ..sym relocations: the more
589 * complicated kinds. In shared-library writing, some relocations
590 * with respect to global symbols must refer to the precise symbol
591 * rather than referring to an offset from the base of the section
592 * _containing_ the symbol. Such relocations call to this routine,
593 * which searches the symbol list for the symbol in question.
595 * R_X86_64_GOT32 references require the _exact_ symbol address to be
596 * used; R_X86_64_32 references can be at an offset from the symbol.
597 * The boolean argument `exact' tells us this.
599 * Return value is the adjusted value of `addr', having become an
600 * offset from the symbol rather than the section. Should always be
601 * zero when returning from an exact call.
603 * Limitation: if you define two symbols at the same place,
604 * confusion will occur.
606 * Inefficiency: we search, currently, using a linked list which
607 * isn't even necessarily sorted.
609 static void elf_add_gsym_reloc(struct elf_section *sect,
610 int32_t segment, uint32_t offset, int32_t pcrel,
611 int type, bool exact)
613 struct elf_reloc *r;
614 struct elf_section *s;
615 struct elf_symbol *sym;
616 struct rbtree *srb;
617 int i;
620 * First look up the segment/offset pair and find a global
621 * symbol corresponding to it. If it's not one of our segments,
622 * then it must be an external symbol, in which case we're fine
623 * doing a normal elf_add_reloc after first sanity-checking
624 * that the offset from the symbol is zero.
626 s = NULL;
627 for (i = 0; i < nsects; i++)
628 if (segment == sects[i]->index) {
629 s = sects[i];
630 break;
633 if (!s) {
634 if (exact && offset)
635 nasm_error(ERR_NONFATAL, "invalid access to an external symbol");
636 else
637 elf_add_reloc(sect, segment, offset - pcrel, type);
638 return;
641 srb = rb_search(s->gsyms, offset);
642 if (!srb || (exact && srb->key != offset)) {
643 nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
644 " for this reference");
645 return;
647 sym = container_of(srb, struct elf_symbol, symv);
649 r = *sect->tail = nasm_malloc(sizeof(struct elf_reloc));
650 sect->tail = &r->next;
651 r->next = NULL;
653 r->address = sect->len;
654 r->offset = offset - pcrel - sym->symv.key;
655 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
656 r->type = type;
658 sect->nrelocs++;
661 static void elf_out(int32_t segto, const void *data,
662 enum out_type type, uint64_t size,
663 int32_t segment, int32_t wrt)
665 struct elf_section *s;
666 int32_t addr;
667 int reltype, bytes;
668 int i;
669 static struct symlininfo sinfo;
671 #if defined(DEBUG) && DEBUG>2
672 if (data)
673 nasm_error(ERR_DEBUG,
674 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64" data: %"PRIx64"\n",
675 currentline, type, segment, segto, size, *(int64_t *)data);
676 else
677 nasm_error(ERR_DEBUG,
678 " elf_out line: %d type: %x seg: %"PRIx32" segto: %"PRIx32" bytes: %"PRIx64"\n",
679 currentline, type, segment, segto, size);
680 #endif
683 * handle absolute-assembly (structure definitions)
685 if (segto == NO_SEG) {
686 if (type != OUT_RESERVE)
687 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
688 " space");
689 return;
692 s = NULL;
693 for (i = 0; i < nsects; i++)
694 if (segto == sects[i]->index) {
695 s = sects[i];
696 break;
698 if (!s) {
699 int tempint; /* ignored */
700 if (segto != elf_section_names(".text", 2, &tempint))
701 nasm_panic(0, "strange segment conditions in ELF driver");
702 else {
703 s = sects[nsects - 1];
704 i = nsects - 1;
708 /* again some stabs debugging stuff */
709 sinfo.offset = s->len;
710 sinfo.section = i;
711 sinfo.segto = segto;
712 sinfo.name = s->name;
713 dfmt->debug_output(TY_DEBUGSYMLIN, &sinfo);
714 /* end of debugging stuff */
716 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
717 nasm_error(ERR_WARNING, "attempt to initialize memory in"
718 " BSS section `%s': ignored", s->name);
719 s->len += realsize(type, size);
720 return;
723 switch (type) {
724 case OUT_RESERVE:
725 if (s->type == SHT_PROGBITS) {
726 nasm_error(ERR_WARNING, "uninitialized space declared in"
727 " non-BSS section `%s': zeroing", s->name);
728 elf_sect_write(s, NULL, size);
729 } else
730 s->len += size;
731 break;
733 case OUT_RAWDATA:
734 if (segment != NO_SEG)
735 nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
736 elf_sect_write(s, data, size);
737 break;
739 case OUT_ADDRESS:
741 int isize = (int)size;
742 int asize = abs((int)size);
744 addr = *(int64_t *)data;
745 if (segment == NO_SEG) {
746 /* Do nothing */
747 } else if (segment % 2) {
748 nasm_error(ERR_NONFATAL, "ELF format does not support"
749 " segment base references");
750 } else {
751 if (wrt == NO_SEG) {
752 switch (isize) {
753 case 1:
754 case -1:
755 elf_add_reloc(s, segment, addr, R_X86_64_8);
756 break;
757 case 2:
758 case -2:
759 elf_add_reloc(s, segment, addr, R_X86_64_16);
760 break;
761 case 4:
762 elf_add_reloc(s, segment, addr, R_X86_64_32);
763 break;
764 case -4:
765 elf_add_reloc(s, segment, addr, R_X86_64_32S);
766 break;
767 case 8:
768 case -8:
769 elf_add_reloc(s, segment, addr, R_X86_64_64);
770 break;
771 default:
772 nasm_panic(0, "internal error elfx32-hpa-871");
773 break;
775 addr = 0;
776 } else if (wrt == elf_gotpc_sect + 1) {
778 * The user will supply GOT relative to $$. ELF
779 * will let us have GOT relative to $. So we
780 * need to fix up the data item by $-$$.
782 addr += s->len;
783 elf_add_reloc(s, segment, addr, R_X86_64_GOTPC32);
784 addr = 0;
785 } else if (wrt == elf_gotoff_sect + 1) {
786 nasm_error(ERR_NONFATAL, "ELFX32 doesn't support "
787 "R_X86_64_GOTOFF64");
788 } else if (wrt == elf_got_sect + 1) {
789 switch (asize) {
790 case 4:
791 elf_add_gsym_reloc(s, segment, addr, 0,
792 R_X86_64_GOT32, true);
793 addr = 0;
794 break;
795 default:
796 nasm_error(ERR_NONFATAL, "invalid ..got reference");
797 break;
799 } else if (wrt == elf_sym_sect + 1) {
800 switch (isize) {
801 case 1:
802 case -1:
803 elf_add_gsym_reloc(s, segment, addr, 0,
804 R_X86_64_8, false);
805 addr = 0;
806 break;
807 case 2:
808 case -2:
809 elf_add_gsym_reloc(s, segment, addr, 0,
810 R_X86_64_16, false);
811 addr = 0;
812 break;
813 case 4:
814 elf_add_gsym_reloc(s, segment, addr, 0,
815 R_X86_64_32, false);
816 addr = 0;
817 break;
818 case -4:
819 elf_add_gsym_reloc(s, segment, addr, 0,
820 R_X86_64_32S, false);
821 addr = 0;
822 break;
823 case 8:
824 case -8:
825 elf_add_gsym_reloc(s, segment, addr, 0,
826 R_X86_64_64, false);
827 addr = 0;
828 break;
829 default:
830 nasm_panic(0, "internal error elfx32-hpa-903");
831 break;
833 } else if (wrt == elf_plt_sect + 1) {
834 nasm_error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
835 "relative PLT references");
836 } else {
837 nasm_error(ERR_NONFATAL, "ELF format does not support this"
838 " use of WRT");
841 elf_sect_writeaddr(s, addr, asize);
842 break;
845 case OUT_REL1ADR:
846 reltype = R_X86_64_PC8;
847 bytes = 1;
848 goto rel12adr;
850 case OUT_REL2ADR:
851 reltype = R_X86_64_PC16;
852 bytes = 2;
853 goto rel12adr;
855 rel12adr:
856 addr = *(int64_t *)data - size;
857 if (segment == segto)
858 nasm_panic(0, "intra-segment OUT_REL1ADR");
859 if (segment == NO_SEG) {
860 /* Do nothing */
861 } else if (segment % 2) {
862 nasm_error(ERR_NONFATAL, "ELF format does not support"
863 " segment base references");
864 } else {
865 if (wrt == NO_SEG) {
866 elf_add_reloc(s, segment, addr, reltype);
867 addr = 0;
868 } else {
869 nasm_error(ERR_NONFATAL,
870 "Unsupported non-32-bit ELF relocation");
873 elf_sect_writeaddr(s, addr, bytes);
874 break;
876 case OUT_REL4ADR:
877 addr = *(int64_t *)data - size;
878 if (segment == segto)
879 nasm_panic(0, "intra-segment OUT_REL4ADR");
880 if (segment == NO_SEG) {
881 /* Do nothing */
882 } else if (segment % 2) {
883 nasm_error(ERR_NONFATAL, "ELFX32 format does not support"
884 " segment base references");
885 } else {
886 if (wrt == NO_SEG) {
887 elf_add_reloc(s, segment, addr, R_X86_64_PC32);
888 addr = 0;
889 } else if (wrt == elf_plt_sect + 1) {
890 elf_add_gsym_reloc(s, segment, addr+size, size,
891 R_X86_64_PLT32, true);
892 addr = 0;
893 } else if (wrt == elf_gotpc_sect + 1 ||
894 wrt == elf_got_sect + 1) {
895 elf_add_gsym_reloc(s, segment, addr+size, size,
896 R_X86_64_GOTPCREL, true);
897 addr = 0;
898 } else if (wrt == elf_gotoff_sect + 1 ||
899 wrt == elf_got_sect + 1) {
900 nasm_error(ERR_NONFATAL, "invalid ..gotoff reference");
901 } else if (wrt == elf_gottpoff_sect + 1) {
902 elf_add_gsym_reloc(s, segment, addr+size, size,
903 R_X86_64_GOTTPOFF, true);
904 addr = 0;
905 } else {
906 nasm_error(ERR_NONFATAL, "ELFX32 format does not support this"
907 " use of WRT");
910 elf_sect_writeaddr(s, addr, 4);
911 break;
913 case OUT_REL8ADR:
914 nasm_error(ERR_NONFATAL,
915 "32-bit ELF format does not support 64-bit relocations");
916 addr = 0;
917 elf_sect_writeaddr(s, addr, 8);
918 break;
922 static void elf_write(void)
924 int align;
925 char *p;
926 int i;
928 struct SAA *symtab;
929 int32_t symtablen, symtablocal;
932 * Work out how many sections we will have. We have SHN_UNDEF,
933 * then the flexible user sections, then the fixed sections
934 * `.shstrtab', `.symtab' and `.strtab', then optionally
935 * relocation sections for the user sections.
937 nsections = sec_numspecial + 1;
938 if (dfmt == &df_stabs)
939 nsections += 3;
940 else if (dfmt == &df_dwarf)
941 nsections += 10;
943 add_sectname("", ".shstrtab");
944 add_sectname("", ".symtab");
945 add_sectname("", ".strtab");
946 for (i = 0; i < nsects; i++) {
947 nsections++; /* for the section itself */
948 if (sects[i]->head) {
949 nsections++; /* for its relocations */
950 add_sectname(".rela", sects[i]->name);
954 if (dfmt == &df_stabs) {
955 /* in case the debug information is wanted, just add these three sections... */
956 add_sectname("", ".stab");
957 add_sectname("", ".stabstr");
958 add_sectname(".rel", ".stab");
959 } else if (dfmt == &df_dwarf) {
960 /* the dwarf debug standard specifies the following ten sections,
961 not all of which are currently implemented,
962 although all of them are defined. */
963 add_sectname("", ".debug_aranges");
964 add_sectname(".rela", ".debug_aranges");
965 add_sectname("", ".debug_pubnames");
966 add_sectname("", ".debug_info");
967 add_sectname(".rela", ".debug_info");
968 add_sectname("", ".debug_abbrev");
969 add_sectname("", ".debug_line");
970 add_sectname(".rela", ".debug_line");
971 add_sectname("", ".debug_frame");
972 add_sectname("", ".debug_loc");
976 * Output the ELF header.
978 nasm_write("\177ELF\1\1\1", 7, ofile);
979 fputc(elf_osabi, ofile);
980 fputc(elf_abiver, ofile);
981 fwritezero(7, ofile);
982 fwriteint16_t(ET_REL, ofile); /* relocatable file */
983 fwriteint16_t(EM_X86_64, ofile); /* processor ID */
984 fwriteint32_t(1L, ofile); /* EV_CURRENT file format version */
985 fwriteint32_t(0L, ofile); /* no entry point */
986 fwriteint32_t(0L, ofile); /* no program header table */
987 fwriteint32_t(0x40L, ofile); /* section headers straight after
988 * ELF header plus alignment */
989 fwriteint32_t(0L, ofile); /* X86_64 defines no special flags */
990 fwriteint16_t(0x34, ofile); /* size of ELF header */
991 fwriteint16_t(0, ofile); /* no program header table, again */
992 fwriteint16_t(0, ofile); /* still no program header table */
993 fwriteint16_t(sizeof(Elf32_Shdr), ofile); /* size of section header */
994 fwriteint16_t(nsections, ofile); /* number of sections */
995 fwriteint16_t(sec_shstrtab, ofile); /* string table section index for
996 * section header table */
997 fwriteint32_t(0L, ofile); /* align to 0x40 bytes */
998 fwriteint32_t(0L, ofile);
999 fwriteint32_t(0L, ofile);
1002 * Build the symbol table and relocation tables.
1004 symtab = elf_build_symtab(&symtablen, &symtablocal);
1005 for (i = 0; i < nsects; i++)
1006 if (sects[i]->head)
1007 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1008 sects[i]->head);
1011 * Now output the section header table.
1014 elf_foffs = 0x40 + sizeof(Elf32_Shdr) * nsections;
1015 align = ALIGN(elf_foffs, SEC_FILEALIGN) - elf_foffs;
1016 elf_foffs += align;
1017 elf_nsect = 0;
1018 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1020 /* SHN_UNDEF */
1021 elf_section_header(0, SHT_NULL, 0, NULL, false, 0, SHN_UNDEF, 0, 0, 0);
1022 p = shstrtab + 1;
1024 /* The normal sections */
1025 for (i = 0; i < nsects; i++) {
1026 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1027 (sects[i]->type == SHT_PROGBITS ?
1028 sects[i]->data : NULL), true,
1029 sects[i]->len, 0, 0, sects[i]->align, 0);
1030 p += strlen(p) + 1;
1033 /* .shstrtab */
1034 elf_section_header(p - shstrtab, SHT_STRTAB, 0, shstrtab, false,
1035 shstrtablen, 0, 0, 1, 0);
1036 p += strlen(p) + 1;
1038 /* .symtab */
1039 elf_section_header(p - shstrtab, SHT_SYMTAB, 0, symtab, true,
1040 symtablen, sec_strtab, symtablocal, 4, 16);
1041 p += strlen(p) + 1;
1043 /* .strtab */
1044 elf_section_header(p - shstrtab, SHT_STRTAB, 0, strs, true,
1045 strslen, 0, 0, 1, 0);
1046 p += strlen(p) + 1;
1048 /* The relocation sections */
1049 for (i = 0; i < nsects; i++)
1050 if (sects[i]->head) {
1051 elf_section_header(p - shstrtab, SHT_RELA, 0, sects[i]->rel, true,
1052 sects[i]->rellen, sec_symtab, i + 1, 4, 12);
1053 p += strlen(p) + 1;
1056 if (dfmt == &df_stabs) {
1057 /* for debugging information, create the last three sections
1058 which are the .stab , .stabstr and .rel.stab sections respectively */
1060 /* this function call creates the stab sections in memory */
1061 stabs_generate();
1063 if (stabbuf && stabstrbuf && stabrelbuf) {
1064 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, stabbuf, false,
1065 stablen, sec_stabstr, 0, 4, 12);
1066 p += strlen(p) + 1;
1068 elf_section_header(p - shstrtab, SHT_STRTAB, 0, stabstrbuf, false,
1069 stabstrlen, 0, 0, 4, 0);
1070 p += strlen(p) + 1;
1072 /* link -> symtable info -> section to refer to */
1073 elf_section_header(p - shstrtab, SHT_REL, 0, stabrelbuf, false,
1074 stabrellen, sec_symtab, sec_stab, 4, 8);
1075 p += strlen(p) + 1;
1077 } else if (dfmt == &df_dwarf) {
1078 /* for dwarf debugging information, create the ten dwarf sections */
1080 /* this function call creates the dwarf sections in memory */
1081 if (dwarf_fsect)
1082 dwarf_generate();
1084 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1085 arangeslen, 0, 0, 1, 0);
1086 p += strlen(p) + 1;
1088 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1089 arangesrellen, sec_symtab, sec_debug_aranges, 1, 12);
1090 p += strlen(p) + 1;
1092 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1093 pubnameslen, 0, 0, 1, 0);
1094 p += strlen(p) + 1;
1096 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1097 infolen, 0, 0, 1, 0);
1098 p += strlen(p) + 1;
1100 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1101 inforellen, sec_symtab, sec_debug_info, 1, 12);
1102 p += strlen(p) + 1;
1104 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1105 abbrevlen, 0, 0, 1, 0);
1106 p += strlen(p) + 1;
1108 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1109 linelen, 0, 0, 1, 0);
1110 p += strlen(p) + 1;
1112 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1113 linerellen, sec_symtab, sec_debug_line, 1, 12);
1114 p += strlen(p) + 1;
1116 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1117 framelen, 0, 0, 8, 0);
1118 p += strlen(p) + 1;
1120 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1121 loclen, 0, 0, 1, 0);
1122 p += strlen(p) + 1;
1124 fwritezero(align, ofile);
1127 * Now output the sections.
1129 elf_write_sections();
1131 nasm_free(elf_sects);
1132 saa_free(symtab);
1135 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1137 struct SAA *s = saa_init(1L);
1138 struct elf_symbol *sym;
1139 uint8_t entry[24], *p;
1140 int i;
1142 *len = *local = 0;
1145 * First, an all-zeros entry, required by the ELF spec.
1147 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1148 *len += 16;
1149 (*local)++;
1152 * Next, an entry for the file name.
1154 p = entry;
1155 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1156 WRITELONG(p, 0); /* no value */
1157 WRITELONG(p, 0); /* no size either */
1158 WRITESHORT(p, STT_FILE); /* type FILE */
1159 WRITESHORT(p, SHN_ABS);
1160 saa_wbytes(s, entry, 16L);
1161 *len += 16;
1162 (*local)++;
1165 * Now some standard symbols defining the segments, for relocation
1166 * purposes.
1168 for (i = 1; i <= nsects; i++) {
1169 p = entry;
1170 WRITELONG(p, 0); /* no symbol name */
1171 WRITELONG(p, 0); /* offset zero */
1172 WRITELONG(p, 0); /* size zero */
1173 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1174 WRITESHORT(p, i); /* section id */
1175 saa_wbytes(s, entry, 16L);
1176 *len += 16;
1177 (*local)++;
1181 * Now the other local symbols.
1183 saa_rewind(syms);
1184 while ((sym = saa_rstruct(syms))) {
1185 if (sym->type & SYM_GLOBAL)
1186 continue;
1187 p = entry;
1188 WRITELONG(p, sym->strpos); /* index into symbol string table */
1189 WRITELONG(p, sym->symv.key); /* value of symbol */
1190 WRITELONG(p, sym->size); /* size of symbol */
1191 WRITECHAR(p, sym->type); /* type and binding */
1192 WRITECHAR(p, sym->other); /* visibility */
1193 WRITESHORT(p, sym->section); /* index into section header table */
1194 saa_wbytes(s, entry, 16L);
1195 *len += 16;
1196 (*local)++;
1199 * dwarf needs symbols for debug sections
1200 * which are relocation targets.
1202 if (dfmt == &df_dwarf) {
1203 dwarf_infosym = *local;
1204 p = entry;
1205 WRITELONG(p, 0); /* no symbol name */
1206 WRITELONG(p, 0); /* offset zero */
1207 WRITELONG(p, 0); /* size zero */
1208 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1209 WRITESHORT(p, sec_debug_info); /* section id */
1210 saa_wbytes(s, entry, 16L);
1211 *len += 16;
1212 (*local)++;
1213 dwarf_abbrevsym = *local;
1214 p = entry;
1215 WRITELONG(p, 0); /* no symbol name */
1216 WRITELONG(p, 0); /* offset zero */
1217 WRITELONG(p, 0); /* size zero */
1218 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1219 WRITESHORT(p, sec_debug_abbrev); /* section id */
1220 saa_wbytes(s, entry, 16L);
1221 *len += 16;
1222 (*local)++;
1223 dwarf_linesym = *local;
1224 p = entry;
1225 WRITELONG(p, 0); /* no symbol name */
1226 WRITELONG(p, 0); /* offset zero */
1227 WRITELONG(p, 0); /* size zero */
1228 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1229 WRITESHORT(p, sec_debug_line); /* section id */
1230 saa_wbytes(s, entry, 16L);
1231 *len += 16;
1232 (*local)++;
1236 * Now the global symbols.
1238 saa_rewind(syms);
1239 while ((sym = saa_rstruct(syms))) {
1240 if (!(sym->type & SYM_GLOBAL))
1241 continue;
1242 p = entry;
1243 WRITELONG(p, sym->strpos);
1244 WRITELONG(p, sym->symv.key);
1245 WRITELONG(p, sym->size);
1246 WRITECHAR(p, sym->type); /* type and binding */
1247 WRITECHAR(p, sym->other); /* visibility */
1248 WRITESHORT(p, sym->section);
1249 saa_wbytes(s, entry, 16L);
1250 *len += 16;
1253 return s;
1256 static struct SAA *elf_build_reltab(uint64_t *len, struct elf_reloc *r)
1258 struct SAA *s;
1259 uint8_t *p, entry[12];
1260 int32_t global_offset;
1262 if (!r)
1263 return NULL;
1265 s = saa_init(1L);
1266 *len = 0;
1269 * How to onvert from a global placeholder to a real symbol index;
1270 * the +2 refers to the two special entries, the null entry and
1271 * the filename entry.
1273 global_offset = -GLOBAL_TEMP_BASE + nsects + nlocals + ndebugs + 2;
1275 while (r) {
1276 int32_t sym = r->symbol;
1278 if (sym >= GLOBAL_TEMP_BASE)
1279 sym += global_offset;
1281 p = entry;
1282 WRITELONG(p, r->address);
1283 WRITELONG(p, (sym << 8) + r->type);
1284 WRITELONG(p, r->offset);
1285 saa_wbytes(s, entry, 12L);
1286 *len += 12;
1288 r = r->next;
1291 return s;
1294 static void elf_section_header(int name, int type, uint64_t flags,
1295 void *data, bool is_saa, uint64_t datalen,
1296 int link, int info, int align, int eltsize)
1298 elf_sects[elf_nsect].data = data;
1299 elf_sects[elf_nsect].len = datalen;
1300 elf_sects[elf_nsect].is_saa = is_saa;
1301 elf_nsect++;
1303 fwriteint32_t((int32_t)name, ofile);
1304 fwriteint32_t((int32_t)type, ofile);
1305 fwriteint32_t((int32_t)flags, ofile);
1306 fwriteint32_t(0L, ofile); /* no address, ever, in object files */
1307 fwriteint32_t(type == 0 ? 0L : elf_foffs, ofile);
1308 fwriteint32_t(datalen, ofile);
1309 if (data)
1310 elf_foffs += ALIGN(datalen, SEC_FILEALIGN);
1311 fwriteint32_t((int32_t)link, ofile);
1312 fwriteint32_t((int32_t)info, ofile);
1313 fwriteint32_t((int32_t)align, ofile);
1314 fwriteint32_t((int32_t)eltsize, ofile);
1317 static void elf_write_sections(void)
1319 int i;
1320 for (i = 0; i < elf_nsect; i++)
1321 if (elf_sects[i].data) {
1322 int32_t len = elf_sects[i].len;
1323 int32_t reallen = ALIGN(len, SEC_FILEALIGN);
1324 int32_t align = reallen - len;
1325 if (elf_sects[i].is_saa)
1326 saa_fpwrite(elf_sects[i].data, ofile);
1327 else
1328 nasm_write(elf_sects[i].data, len, ofile);
1329 fwritezero(align, ofile);
1333 static void elf_sect_write(struct elf_section *sect, const void *data, size_t len)
1335 saa_wbytes(sect->data, data, len);
1336 sect->len += len;
1339 static void elf_sect_writeaddr(struct elf_section *sect, int64_t data, size_t len)
1341 saa_writeaddr(sect->data, data, len);
1342 sect->len += len;
1345 static void elf_sectalign(int32_t seg, unsigned int value)
1347 struct elf_section *s = NULL;
1348 int i;
1350 for (i = 0; i < nsects; i++) {
1351 if (sects[i]->index == seg) {
1352 s = sects[i];
1353 break;
1356 if (!s || !is_power2(value))
1357 return;
1359 if (value > s->align)
1360 s->align = value;
1363 static int32_t elf_segbase(int32_t segment)
1365 return segment;
1368 static void elf_filename(char *inname, char *outname)
1370 strcpy(elf_module, inname);
1371 standard_extension(inname, outname, ".o");
1374 extern macros_t elf_stdmac[];
1376 static int elf_set_info(enum geninfo type, char **val)
1378 (void)type;
1379 (void)val;
1380 return 0;
1382 static const struct dfmt df_dwarf = {
1383 "ELFX32 (x86-64) dwarf debug format for Linux/Unix",
1384 "dwarf",
1385 dwarf_init,
1386 dwarf_linenum,
1387 null_debug_deflabel,
1388 null_debug_directive,
1389 debug_typevalue,
1390 dwarf_output,
1391 dwarf_cleanup
1393 static const struct dfmt df_stabs = {
1394 "ELFX32 (x86-64) stabs debug format for Linux/Unix",
1395 "stabs",
1396 null_debug_init,
1397 stabs_linenum,
1398 null_debug_deflabel,
1399 null_debug_directive,
1400 debug_typevalue,
1401 stabs_output,
1402 stabs_cleanup
1405 static const struct dfmt * const elfx32_debugs_arr[3] =
1406 { &df_dwarf, &df_stabs, NULL };
1408 const struct ofmt of_elfx32 = {
1409 "ELFX32 (x86_64) object files (e.g. Linux)",
1410 "elfx32",
1413 elfx32_debugs_arr,
1414 &df_stabs,
1415 elf_stdmac,
1416 elf_init,
1417 elf_set_info,
1418 elf_out,
1419 elf_deflabel,
1420 elf_section_names,
1421 elf_sectalign,
1422 elf_segbase,
1423 elf_directive,
1424 elf_filename,
1425 elf_cleanup
1428 /* common debugging routines */
1429 static void debug_typevalue(int32_t type)
1431 int32_t stype, ssize;
1432 switch (TYM_TYPE(type)) {
1433 case TY_LABEL:
1434 ssize = 0;
1435 stype = STT_NOTYPE;
1436 break;
1437 case TY_BYTE:
1438 ssize = 1;
1439 stype = STT_OBJECT;
1440 break;
1441 case TY_WORD:
1442 ssize = 2;
1443 stype = STT_OBJECT;
1444 break;
1445 case TY_DWORD:
1446 ssize = 4;
1447 stype = STT_OBJECT;
1448 break;
1449 case TY_FLOAT:
1450 ssize = 4;
1451 stype = STT_OBJECT;
1452 break;
1453 case TY_QWORD:
1454 ssize = 8;
1455 stype = STT_OBJECT;
1456 break;
1457 case TY_TBYTE:
1458 ssize = 10;
1459 stype = STT_OBJECT;
1460 break;
1461 case TY_OWORD:
1462 ssize = 16;
1463 stype = STT_OBJECT;
1464 break;
1465 case TY_YWORD:
1466 ssize = 32;
1467 stype = STT_OBJECT;
1468 break;
1469 case TY_COMMON:
1470 ssize = 0;
1471 stype = STT_COMMON;
1472 break;
1473 case TY_SEG:
1474 ssize = 0;
1475 stype = STT_SECTION;
1476 break;
1477 case TY_EXTERN:
1478 ssize = 0;
1479 stype = STT_NOTYPE;
1480 break;
1481 case TY_EQU:
1482 ssize = 0;
1483 stype = STT_NOTYPE;
1484 break;
1485 default:
1486 ssize = 0;
1487 stype = STT_NOTYPE;
1488 break;
1490 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1491 lastsym->size = ssize;
1492 lastsym->type = stype;
1496 /* stabs debugging routines */
1498 static void stabs_linenum(const char *filename, int32_t linenumber, int32_t segto)
1500 (void)segto;
1501 if (!stabs_filename) {
1502 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1503 strcpy(stabs_filename, filename);
1504 } else {
1505 if (strcmp(stabs_filename, filename)) {
1506 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1507 in fact, this leak comes in quite handy to maintain a list of files
1508 encountered so far in the symbol lines... */
1510 /* why not nasm_free(stabs_filename); we're done with the old one */
1512 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1513 strcpy(stabs_filename, filename);
1516 debug_immcall = 1;
1517 currentline = linenumber;
1520 static void stabs_output(int type, void *param)
1522 struct symlininfo *s;
1523 struct linelist *el;
1524 if (type == TY_DEBUGSYMLIN) {
1525 if (debug_immcall) {
1526 s = (struct symlininfo *)param;
1527 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1528 return; /* line info is only collected for executable sections */
1529 numlinestabs++;
1530 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1531 el->info.offset = s->offset;
1532 el->info.section = s->section;
1533 el->info.name = s->name;
1534 el->line = currentline;
1535 el->filename = stabs_filename;
1536 el->next = 0;
1537 if (stabslines) {
1538 stabslines->last->next = el;
1539 stabslines->last = el;
1540 } else {
1541 stabslines = el;
1542 stabslines->last = el;
1546 debug_immcall = 0;
1549 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1551 static void stabs_generate(void)
1553 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1554 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1555 char **allfiles;
1556 int *fileidx;
1558 struct linelist *ptr;
1560 ptr = stabslines;
1562 allfiles = (char **)nasm_zalloc(numlinestabs * sizeof(char *));
1563 numfiles = 0;
1564 while (ptr) {
1565 if (numfiles == 0) {
1566 allfiles[0] = ptr->filename;
1567 numfiles++;
1568 } else {
1569 for (i = 0; i < numfiles; i++) {
1570 if (!strcmp(allfiles[i], ptr->filename))
1571 break;
1573 if (i >= numfiles) {
1574 allfiles[i] = ptr->filename;
1575 numfiles++;
1578 ptr = ptr->next;
1580 strsize = 1;
1581 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1582 for (i = 0; i < numfiles; i++) {
1583 fileidx[i] = strsize;
1584 strsize += strlen(allfiles[i]) + 1;
1586 mainfileindex = 0;
1587 for (i = 0; i < numfiles; i++) {
1588 if (!strcmp(allfiles[i], elf_module)) {
1589 mainfileindex = i;
1590 break;
1595 * worst case size of the stab buffer would be:
1596 * the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1597 * plus one "ending" entry
1599 sbuf = (uint8_t *)nasm_malloc((numlinestabs * 2 + 4) *
1600 sizeof(struct stabentry));
1601 ssbuf = (uint8_t *)nasm_malloc(strsize);
1602 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1603 rptr = rbuf;
1605 for (i = 0; i < numfiles; i++)
1606 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1607 ssbuf[0] = 0;
1609 stabstrlen = strsize; /* set global variable for length of stab strings */
1611 sptr = sbuf;
1612 ptr = stabslines;
1613 numstabs = 0;
1615 if (ptr) {
1617 * this is the first stab, its strx points to the filename of the
1618 * the source-file, the n_desc field should be set to the number
1619 * of remaining stabs
1621 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, stabstrlen);
1623 /* this is the stab for the main source file */
1624 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1626 /* relocation table entry */
1629 * Since the symbol table has two entries before
1630 * the section symbols, the index in the info.section
1631 * member must be adjusted by adding 2
1634 WRITELONG(rptr, (sptr - sbuf) - 4);
1635 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1637 numstabs++;
1638 currfile = mainfileindex;
1641 while (ptr) {
1642 if (strcmp(allfiles[currfile], ptr->filename)) {
1643 /* oops file has changed... */
1644 for (i = 0; i < numfiles; i++)
1645 if (!strcmp(allfiles[i], ptr->filename))
1646 break;
1647 currfile = i;
1648 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1649 ptr->info.offset);
1650 numstabs++;
1652 /* relocation table entry */
1654 WRITELONG(rptr, (sptr - sbuf) - 4);
1655 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1658 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1659 numstabs++;
1661 /* relocation table entry */
1663 WRITELONG(rptr, (sptr - sbuf) - 4);
1664 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_X86_64_32);
1666 ptr = ptr->next;
1670 /* this is an "ending" token */
1671 WRITE_STAB(sptr, 0, N_SO, 0, 0, 0);
1672 numstabs++;
1674 ((struct stabentry *)sbuf)->n_desc = numstabs;
1676 nasm_free(allfiles);
1677 nasm_free(fileidx);
1679 stablen = (sptr - sbuf);
1680 stabrellen = (rptr - rbuf);
1681 stabrelbuf = rbuf;
1682 stabbuf = sbuf;
1683 stabstrbuf = ssbuf;
1686 static void stabs_cleanup(void)
1688 struct linelist *ptr, *del;
1689 if (!stabslines)
1690 return;
1692 ptr = stabslines;
1693 while (ptr) {
1694 del = ptr;
1695 ptr = ptr->next;
1696 nasm_free(del);
1699 nasm_free(stabbuf);
1700 nasm_free(stabrelbuf);
1701 nasm_free(stabstrbuf);
1704 /* dwarf routines */
1706 static void dwarf_init(void)
1708 ndebugs = 3; /* 3 debug symbols */
1711 static void dwarf_linenum(const char *filename, int32_t linenumber,
1712 int32_t segto)
1714 (void)segto;
1715 dwarf_findfile(filename);
1716 debug_immcall = 1;
1717 currentline = linenumber;
1720 /* called from elf_out with type == TY_DEBUGSYMLIN */
1721 static void dwarf_output(int type, void *param)
1723 int ln, aa, inx, maxln, soc;
1724 struct symlininfo *s;
1725 struct SAA *plinep;
1727 (void)type;
1729 s = (struct symlininfo *)param;
1731 /* line number info is only gathered for executable sections */
1732 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1733 return;
1735 /* Check if section index has changed */
1736 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1737 dwarf_findsect(s->section);
1739 /* do nothing unless line or file has changed */
1740 if (!debug_immcall)
1741 return;
1743 ln = currentline - dwarf_csect->line;
1744 aa = s->offset - dwarf_csect->offset;
1745 inx = dwarf_clist->line;
1746 plinep = dwarf_csect->psaa;
1747 /* check for file change */
1748 if (!(inx == dwarf_csect->file)) {
1749 saa_write8(plinep,DW_LNS_set_file);
1750 saa_write8(plinep,inx);
1751 dwarf_csect->file = inx;
1753 /* check for line change */
1754 if (ln) {
1755 /* test if in range of special op code */
1756 maxln = line_base + line_range;
1757 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1758 if (ln >= line_base && ln < maxln && soc < 256) {
1759 saa_write8(plinep,soc);
1760 } else {
1761 saa_write8(plinep,DW_LNS_advance_line);
1762 saa_wleb128s(plinep,ln);
1763 if (aa) {
1764 saa_write8(plinep,DW_LNS_advance_pc);
1765 saa_wleb128u(plinep,aa);
1768 dwarf_csect->line = currentline;
1769 dwarf_csect->offset = s->offset;
1772 /* show change handled */
1773 debug_immcall = 0;
1777 static void dwarf_generate(void)
1779 uint8_t *pbuf;
1780 int indx;
1781 struct linelist *ftentry;
1782 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1783 struct SAA *parangesrel, *plinesrel, *pinforel;
1784 struct sectlist *psect;
1785 size_t saalen, linepoff, totlen, highaddr;
1787 /* write epilogues for each line program range */
1788 /* and build aranges section */
1789 paranges = saa_init(1L);
1790 parangesrel = saa_init(1L);
1791 saa_write16(paranges,3); /* dwarf version */
1792 saa_write32(parangesrel, paranges->datalen+4);
1793 saa_write32(parangesrel, (dwarf_infosym << 8) + R_X86_64_32); /* reloc to info */
1794 saa_write32(parangesrel, 0);
1795 saa_write32(paranges,0); /* offset into info */
1796 saa_write8(paranges,4); /* pointer size */
1797 saa_write8(paranges,0); /* not segmented */
1798 saa_write32(paranges,0); /* padding */
1799 /* iterate though sectlist entries */
1800 psect = dwarf_fsect;
1801 totlen = 0;
1802 highaddr = 0;
1803 for (indx = 0; indx < dwarf_nsections; indx++)
1805 plinep = psect->psaa;
1806 /* Line Number Program Epilogue */
1807 saa_write8(plinep,2); /* std op 2 */
1808 saa_write8(plinep,(sects[psect->section]->len)-psect->offset);
1809 saa_write8(plinep,DW_LNS_extended_op);
1810 saa_write8(plinep,1); /* operand length */
1811 saa_write8(plinep,DW_LNE_end_sequence);
1812 totlen += plinep->datalen;
1813 /* range table relocation entry */
1814 saa_write32(parangesrel, paranges->datalen + 4);
1815 saa_write32(parangesrel, ((uint32_t) (psect->section + 2) << 8) + R_X86_64_32);
1816 saa_write32(parangesrel, (uint32_t) 0);
1817 /* range table entry */
1818 saa_write32(paranges,0x0000); /* range start */
1819 saa_write32(paranges,sects[psect->section]->len); /* range length */
1820 highaddr += sects[psect->section]->len;
1821 /* done with this entry */
1822 psect = psect->next;
1824 saa_write32(paranges,0); /* null address */
1825 saa_write32(paranges,0); /* null length */
1826 saalen = paranges->datalen;
1827 arangeslen = saalen + 4;
1828 arangesbuf = pbuf = nasm_malloc(arangeslen);
1829 WRITELONG(pbuf,saalen); /* initial length */
1830 saa_rnbytes(paranges, pbuf, saalen);
1831 saa_free(paranges);
1833 /* build rela.aranges section */
1834 arangesrellen = saalen = parangesrel->datalen;
1835 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1836 saa_rnbytes(parangesrel, pbuf, saalen);
1837 saa_free(parangesrel);
1839 /* build pubnames section */
1840 ppubnames = saa_init(1L);
1841 saa_write16(ppubnames,3); /* dwarf version */
1842 saa_write32(ppubnames,0); /* offset into info */
1843 saa_write32(ppubnames,0); /* space used in info */
1844 saa_write32(ppubnames,0); /* end of list */
1845 saalen = ppubnames->datalen;
1846 pubnameslen = saalen + 4;
1847 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
1848 WRITELONG(pbuf,saalen); /* initial length */
1849 saa_rnbytes(ppubnames, pbuf, saalen);
1850 saa_free(ppubnames);
1852 /* build info section */
1853 pinfo = saa_init(1L);
1854 pinforel = saa_init(1L);
1855 saa_write16(pinfo,3); /* dwarf version */
1856 saa_write32(pinforel, pinfo->datalen + 4);
1857 saa_write32(pinforel, (dwarf_abbrevsym << 8) + R_X86_64_32); /* reloc to abbrev */
1858 saa_write32(pinforel, 0);
1859 saa_write32(pinfo,0); /* offset into abbrev */
1860 saa_write8(pinfo,4); /* pointer size */
1861 saa_write8(pinfo,1); /* abbrviation number LEB128u */
1862 saa_write32(pinforel, pinfo->datalen + 4);
1863 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1864 saa_write32(pinforel, 0);
1865 saa_write32(pinfo,0); /* DW_AT_low_pc */
1866 saa_write32(pinforel, pinfo->datalen + 4);
1867 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1868 saa_write32(pinforel, 0);
1869 saa_write32(pinfo,highaddr); /* DW_AT_high_pc */
1870 saa_write32(pinforel, pinfo->datalen + 4);
1871 saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
1872 saa_write32(pinforel, 0);
1873 saa_write32(pinfo,0); /* DW_AT_stmt_list */
1874 saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
1875 saa_wbytes(pinfo, nasm_signature, strlen(nasm_signature)+1);
1876 saa_write16(pinfo,DW_LANG_Mips_Assembler);
1877 saa_write8(pinfo,2); /* abbrviation number LEB128u */
1878 saa_write32(pinforel, pinfo->datalen + 4);
1879 saa_write32(pinforel, ((dwarf_fsect->section + 2) << 8) + R_X86_64_32);
1880 saa_write32(pinforel, 0);
1881 saa_write32(pinfo,0); /* DW_AT_low_pc */
1882 saa_write32(pinfo,0); /* DW_AT_frame_base */
1883 saa_write8(pinfo,0); /* end of entries */
1884 saalen = pinfo->datalen;
1885 infolen = saalen + 4;
1886 infobuf = pbuf = nasm_malloc(infolen);
1887 WRITELONG(pbuf,saalen); /* initial length */
1888 saa_rnbytes(pinfo, pbuf, saalen);
1889 saa_free(pinfo);
1891 /* build rela.info section */
1892 inforellen = saalen = pinforel->datalen;
1893 inforelbuf = pbuf = nasm_malloc(inforellen);
1894 saa_rnbytes(pinforel, pbuf, saalen);
1895 saa_free(pinforel);
1897 /* build abbrev section */
1898 pabbrev = saa_init(1L);
1899 saa_write8(pabbrev,1); /* entry number LEB128u */
1900 saa_write8(pabbrev,DW_TAG_compile_unit); /* tag LEB128u */
1901 saa_write8(pabbrev,1); /* has children */
1902 /* the following attributes and forms are all LEB128u values */
1903 saa_write8(pabbrev,DW_AT_low_pc);
1904 saa_write8(pabbrev,DW_FORM_addr);
1905 saa_write8(pabbrev,DW_AT_high_pc);
1906 saa_write8(pabbrev,DW_FORM_addr);
1907 saa_write8(pabbrev,DW_AT_stmt_list);
1908 saa_write8(pabbrev,DW_FORM_data4);
1909 saa_write8(pabbrev,DW_AT_name);
1910 saa_write8(pabbrev,DW_FORM_string);
1911 saa_write8(pabbrev,DW_AT_producer);
1912 saa_write8(pabbrev,DW_FORM_string);
1913 saa_write8(pabbrev,DW_AT_language);
1914 saa_write8(pabbrev,DW_FORM_data2);
1915 saa_write16(pabbrev,0); /* end of entry */
1916 /* LEB128u usage same as above */
1917 saa_write8(pabbrev,2); /* entry number */
1918 saa_write8(pabbrev,DW_TAG_subprogram);
1919 saa_write8(pabbrev,0); /* no children */
1920 saa_write8(pabbrev,DW_AT_low_pc);
1921 saa_write8(pabbrev,DW_FORM_addr);
1922 saa_write8(pabbrev,DW_AT_frame_base);
1923 saa_write8(pabbrev,DW_FORM_data4);
1924 saa_write16(pabbrev,0); /* end of entry */
1925 abbrevlen = saalen = pabbrev->datalen;
1926 abbrevbuf = pbuf = nasm_malloc(saalen);
1927 saa_rnbytes(pabbrev, pbuf, saalen);
1928 saa_free(pabbrev);
1930 /* build line section */
1931 /* prolog */
1932 plines = saa_init(1L);
1933 saa_write8(plines,1); /* Minimum Instruction Length */
1934 saa_write8(plines,1); /* Initial value of 'is_stmt' */
1935 saa_write8(plines,line_base); /* Line Base */
1936 saa_write8(plines,line_range); /* Line Range */
1937 saa_write8(plines,opcode_base); /* Opcode Base */
1938 /* standard opcode lengths (# of LEB128u operands) */
1939 saa_write8(plines,0); /* Std opcode 1 length */
1940 saa_write8(plines,1); /* Std opcode 2 length */
1941 saa_write8(plines,1); /* Std opcode 3 length */
1942 saa_write8(plines,1); /* Std opcode 4 length */
1943 saa_write8(plines,1); /* Std opcode 5 length */
1944 saa_write8(plines,0); /* Std opcode 6 length */
1945 saa_write8(plines,0); /* Std opcode 7 length */
1946 saa_write8(plines,0); /* Std opcode 8 length */
1947 saa_write8(plines,1); /* Std opcode 9 length */
1948 saa_write8(plines,0); /* Std opcode 10 length */
1949 saa_write8(plines,0); /* Std opcode 11 length */
1950 saa_write8(plines,1); /* Std opcode 12 length */
1951 /* Directory Table */
1952 saa_write8(plines,0); /* End of table */
1953 /* File Name Table */
1954 ftentry = dwarf_flist;
1955 for (indx = 0; indx < dwarf_numfiles; indx++) {
1956 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
1957 saa_write8(plines,0); /* directory LEB128u */
1958 saa_write8(plines,0); /* time LEB128u */
1959 saa_write8(plines,0); /* size LEB128u */
1960 ftentry = ftentry->next;
1962 saa_write8(plines,0); /* End of table */
1963 linepoff = plines->datalen;
1964 linelen = linepoff + totlen + 10;
1965 linebuf = pbuf = nasm_malloc(linelen);
1966 WRITELONG(pbuf,linelen-4); /* initial length */
1967 WRITESHORT(pbuf,3); /* dwarf version */
1968 WRITELONG(pbuf,linepoff); /* offset to line number program */
1969 /* write line header */
1970 saalen = linepoff;
1971 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
1972 pbuf += linepoff;
1973 saa_free(plines);
1974 /* concatonate line program ranges */
1975 linepoff += 13;
1976 plinesrel = saa_init(1L);
1977 psect = dwarf_fsect;
1978 for (indx = 0; indx < dwarf_nsections; indx++) {
1979 saa_write32(plinesrel, linepoff);
1980 saa_write32(plinesrel, ((psect->section + 2) << 8) + R_X86_64_32);
1981 saa_write32(plinesrel, 0);
1982 plinep = psect->psaa;
1983 saalen = plinep->datalen;
1984 saa_rnbytes(plinep, pbuf, saalen);
1985 pbuf += saalen;
1986 linepoff += saalen;
1987 saa_free(plinep);
1988 /* done with this entry */
1989 psect = psect->next;
1993 /* build rela.lines section */
1994 linerellen =saalen = plinesrel->datalen;
1995 linerelbuf = pbuf = nasm_malloc(linerellen);
1996 saa_rnbytes(plinesrel, pbuf, saalen);
1997 saa_free(plinesrel);
1999 /* build frame section */
2000 framelen = 4;
2001 framebuf = pbuf = nasm_malloc(framelen);
2002 WRITELONG(pbuf,framelen-4); /* initial length */
2004 /* build loc section */
2005 loclen = 16;
2006 locbuf = pbuf = nasm_malloc(loclen);
2007 WRITELONG(pbuf,0); /* null beginning offset */
2008 WRITELONG(pbuf,0); /* null ending offset */
2011 static void dwarf_cleanup(void)
2013 nasm_free(arangesbuf);
2014 nasm_free(arangesrelbuf);
2015 nasm_free(pubnamesbuf);
2016 nasm_free(infobuf);
2017 nasm_free(inforelbuf);
2018 nasm_free(abbrevbuf);
2019 nasm_free(linebuf);
2020 nasm_free(linerelbuf);
2021 nasm_free(framebuf);
2022 nasm_free(locbuf);
2025 static void dwarf_findfile(const char * fname)
2027 int finx;
2028 struct linelist *match;
2030 /* return if fname is current file name */
2031 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
2032 return;
2034 /* search for match */
2035 match = 0;
2036 if (dwarf_flist) {
2037 match = dwarf_flist;
2038 for (finx = 0; finx < dwarf_numfiles; finx++) {
2039 if (!(strcmp(fname, match->filename))) {
2040 dwarf_clist = match;
2041 return;
2046 /* add file name to end of list */
2047 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2048 dwarf_numfiles++;
2049 dwarf_clist->line = dwarf_numfiles;
2050 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2051 strcpy(dwarf_clist->filename,fname);
2052 dwarf_clist->next = 0;
2053 if (!dwarf_flist) { /* if first entry */
2054 dwarf_flist = dwarf_elist = dwarf_clist;
2055 dwarf_clist->last = 0;
2056 } else { /* chain to previous entry */
2057 dwarf_elist->next = dwarf_clist;
2058 dwarf_elist = dwarf_clist;
2062 static void dwarf_findsect(const int index)
2064 int sinx;
2065 struct sectlist *match;
2066 struct SAA *plinep;
2068 /* return if index is current section index */
2069 if (dwarf_csect && (dwarf_csect->section == index))
2070 return;
2072 /* search for match */
2073 match = 0;
2074 if (dwarf_fsect) {
2075 match = dwarf_fsect;
2076 for (sinx = 0; sinx < dwarf_nsections; sinx++) {
2077 if (match->section == index) {
2078 dwarf_csect = match;
2079 return;
2081 match = match->next;
2085 /* add entry to end of list */
2086 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2087 dwarf_nsections++;
2088 dwarf_csect->psaa = plinep = saa_init(1L);
2089 dwarf_csect->line = 1;
2090 dwarf_csect->offset = 0;
2091 dwarf_csect->file = 1;
2092 dwarf_csect->section = index;
2093 dwarf_csect->next = 0;
2094 /* set relocatable address at start of line program */
2095 saa_write8(plinep,DW_LNS_extended_op);
2096 saa_write8(plinep,5); /* operand length */
2097 saa_write8(plinep,DW_LNE_set_address);
2098 saa_write32(plinep,0); /* Start Address */
2100 if (!dwarf_fsect) { /* if first entry */
2101 dwarf_fsect = dwarf_esect = dwarf_csect;
2102 dwarf_csect->last = 0;
2103 } else { /* chain to previous entry */
2104 dwarf_esect->next = dwarf_csect;
2105 dwarf_esect = dwarf_csect;
2109 #endif /* OF_ELFX32 */