1/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
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.
17 *
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.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * Internal definitions common to outelf32 and outelf64
36 */
37#ifndef OUTPUT_OUTELF_H
38#define OUTPUT_OUTELF_H
39
40#include "elf.h"
41#include "rbtree.h"
42#include "saa.h"
43
44/* symbol binding */
45#define SYM_GLOBAL ELF32_ST_MKBIND(STB_GLOBAL)
46#define SYM_LOCAL ELF32_ST_MKBIND(STB_LOCAL)
47
48#define GLOBAL_TEMP_BASE 0x40000000 /* bigger than any sane symbol index */
49
50/* alignment of sections in file */
51#define SEC_FILEALIGN 16
52
53/* this stuff is needed for the dwarf/stabs debugging format */
54#define TY_DEBUGSYMLIN 0x40 /* internal call to debug_out */
55
56/* Known sections with nonstandard defaults */
57struct elf_known_section {
58 const char *name; /* Name of section */
59 int type; /* Section type (SHT_) */
60 uint32_t flags; /* Section flags (SHF_) */
61 uint32_t align; /* Section alignment */
62};
63extern const struct elf_known_section elf_known_sections[];
64
65/*
66 * Special ELF sections (after the real sections but before debugging ones)
67 */
68#define sec_shstrtab (nsects + 1)
69#define sec_symtab (nsects + 2)
70#define sec_strtab (nsects + 3)
71#define sec_numspecial 3
72
73/*
74 * Debugging ELF sections (last in the file)
75 */
76
77/* stabs */
78#define sec_stab (nsections-3)
79#define sec_stabstr (nsections-2)
80#define sec_rel_stab (nsections-1)
81
82/* stabs symbol table format */
83struct stabentry {
84 uint32_t n_strx;
85 uint8_t n_type;
86 uint8_t n_other;
87 uint16_t n_desc;
88 uint32_t n_value;
89};
90
91/* dwarf */
92#define sec_debug_aranges (nsections-10)
93#define sec_rela_debug_aranges (nsections-9)
94#define sec_debug_pubnames (nsections-8)
95#define sec_debug_info (nsections-7)
96#define sec_rela_debug_info (nsections-6)
97#define sec_debug_abbrev (nsections-5)
98#define sec_debug_line (nsections-4)
99#define sec_rela_debug_line (nsections-3)
100#define sec_debug_frame (nsections-2)
101#define sec_debug_loc (nsections-1)
102
103extern uint8_t elf_osabi;
104extern uint8_t elf_abiver;
105
106#define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
107 do { \
108 WRITELONG(p, n_strx); \
109 WRITECHAR(p, n_type); \
110 WRITECHAR(p, n_other); \
111 WRITESHORT(p, n_desc); \
112 WRITELONG(p, n_value); \
113 } while (0)
114
115struct elf_reloc {
116 struct elf_reloc *next;
117 int64_t address; /* relative to _start_ of section */
118 int64_t symbol; /* symbol index */
119 int64_t offset; /* symbol addend */
120 int type; /* type of relocation */
121};
122
123struct elf_symbol {
124 struct rbtree symv; /* symbol value and symbol rbtree */
125 int32_t strpos; /* string table position of name */
126 int32_t section; /* section ID of the symbol */
127 int type; /* symbol type */
128 int other; /* symbol visibility */
129 int32_t size; /* size of symbol */
130 int32_t globnum; /* symbol table offset if global */
131 struct elf_symbol *nextfwd; /* list of unresolved-size symbols */
132 char *name; /* used temporarily if in above list */
133};
134
135struct elf_section {
136 struct SAA *data;
137 uint64_t len;
138 uint64_t size;
139 uint64_t nrelocs;
140 int32_t index;
141 int type; /* SHT_PROGBITS or SHT_NOBITS */
142 uint64_t align; /* alignment: power of two */
143 uint64_t flags; /* section flags */
144 char *name;
145 struct SAA *rel;
146 uint64_t rellen;
147 struct elf_reloc *head;
148 struct elf_reloc **tail;
149 struct rbtree *gsyms; /* global symbols in section */
150};
151
152#endif /* OUTPUT_OUTELF_H */
153