1 |
288 |
jeremybenn |
/* LTO routines for COFF object files.
|
2 |
|
|
Copyright 2009, 2010 Free Software Foundation, Inc.
|
3 |
|
|
Contributed by Dave Korn.
|
4 |
|
|
|
5 |
|
|
This file is part of GCC.
|
6 |
|
|
|
7 |
|
|
GCC is free software; you can redistribute it and/or modify it under
|
8 |
|
|
the terms of the GNU General Public License as published by the Free
|
9 |
|
|
Software Foundation; either version 3, or (at your option) any later
|
10 |
|
|
version.
|
11 |
|
|
|
12 |
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
13 |
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
14 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
15 |
|
|
for more details.
|
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License
|
18 |
|
|
along with GCC; see the file COPYING3. If not see
|
19 |
|
|
<http://www.gnu.org/licenses/>. */
|
20 |
|
|
|
21 |
|
|
#ifndef LTO_COFF_H
|
22 |
|
|
#define LTO_COFF_H
|
23 |
|
|
|
24 |
|
|
/* Rather than implementing a libcoff to match libelf, or attempting to
|
25 |
|
|
integrate libbfd into GCC, this file is a self-contained (and very
|
26 |
|
|
minimal) COFF format object file reader/writer. The generated files
|
27 |
|
|
will contain a COFF header, a number of COFF section headers, the
|
28 |
|
|
section data itself, and a trailing string table for section names. */
|
29 |
|
|
|
30 |
|
|
/* Alignment of sections in a COFF object file.
|
31 |
|
|
|
32 |
|
|
The LTO writer uses zlib compression on the data that it streams into
|
33 |
|
|
LTO sections in the output object file. Because these streams don't
|
34 |
|
|
have any embedded size information, the section in the object file must
|
35 |
|
|
be exactly sized to the data emitted; any trailing padding bytes will
|
36 |
|
|
be interpreted as partial and/or corrupt compressed data.
|
37 |
|
|
|
38 |
|
|
This is easy enough to do on COFF targets (with binutils 2.20.1 or
|
39 |
|
|
above) because we can specify 1-byte alignment for the LTO sections.
|
40 |
|
|
They are then emitted precisely-sized and byte-packed into the object
|
41 |
|
|
and the reader is happy when it parses them later. This is currently
|
42 |
|
|
implemented in the x86/windows backed in i386_pe_asm_named_section()
|
43 |
|
|
in config/i386/winnt.c by detecting the LTO section name prefix,
|
44 |
|
|
|
45 |
|
|
That would be sufficient, but for one thing. At the start of the LTO
|
46 |
|
|
data is a header struct with (currently) a couple of version numbers and
|
47 |
|
|
some type info; see struct lto_header in lto-streamer.h. If the sections
|
48 |
|
|
are byte-packed, this header will not necessarily be correctly-aligned
|
49 |
|
|
when it is read back into memory.
|
50 |
|
|
|
51 |
|
|
On x86 targets, which are currently the only LTO-COFF targets, misaligned
|
52 |
|
|
memory accesses aren't problematic (okay, inefficient, but not worth
|
53 |
|
|
worrying about two half-word memory reads per section in the context of
|
54 |
|
|
everything else the compiler has to do at the time!), but RISC targets may
|
55 |
|
|
fail on trying to access the header struct. In this case, it will be
|
56 |
|
|
necessary to enable (preferably in a target-dependent fashion, but a few
|
57 |
|
|
bytes of padding are hardly an important issue if it comes down to it) the
|
58 |
|
|
COFF_ALIGNMENT macros below.
|
59 |
|
|
|
60 |
|
|
As currently implemented, this will emit padding to the necessary number
|
61 |
|
|
of bytes after each LTO section. These bytes will constitute 'gaps' in
|
62 |
|
|
the object file structure, as they won't be covered by any section header.
|
63 |
|
|
This hasn't yet been tested, because no such RISC LTO-COFF target yet
|
64 |
|
|
exists. If it causes problems further down the toolchain, it will be
|
65 |
|
|
necessary to adapt the code to emit additional section headers for these
|
66 |
|
|
padding bytes, but the odds are that it will "just work".
|
67 |
|
|
|
68 |
|
|
*/
|
69 |
|
|
|
70 |
|
|
#if 0
|
71 |
|
|
#define COFF_ALIGNMENT (4)
|
72 |
|
|
#define COFF_ALIGNMENTM1 (COFF_ALIGNMENT - 1)
|
73 |
|
|
#define COFF_ALIGN(x) (((x) + COFF_ALIGNMENTM1) & ~COFF_ALIGNMENTM1)
|
74 |
|
|
#else
|
75 |
|
|
#define COFF_ALIGNMENT (1)
|
76 |
|
|
#define COFF_ALIGN(x) (x)
|
77 |
|
|
#endif
|
78 |
|
|
|
79 |
|
|
/* COFF header machine codes. */
|
80 |
|
|
|
81 |
|
|
#define IMAGE_FILE_MACHINE_I386 (0x014c)
|
82 |
|
|
|
83 |
|
|
/* Known header magics for validation, as an array initialiser. */
|
84 |
|
|
|
85 |
|
|
#define COFF_KNOWN_MACHINES \
|
86 |
|
|
{ IMAGE_FILE_MACHINE_I386/*, ... add more here when working. */ }
|
87 |
|
|
|
88 |
|
|
/* COFF object file header, section and symbol flags and types. These are
|
89 |
|
|
currently specific to PE-COFF, which is the only LTO-COFF format at the
|
90 |
|
|
time of writing. Maintainers adding support for new COFF formats will
|
91 |
|
|
need to make these into target macros of some kind. */
|
92 |
|
|
|
93 |
|
|
/* COFF header characteristics. */
|
94 |
|
|
|
95 |
|
|
#define IMAGE_FILE_EXECUTABLE_IMAGE (1 << 1)
|
96 |
|
|
#define IMAGE_FILE_32BIT_MACHINE (1 << 8)
|
97 |
|
|
#define IMAGE_FILE_SYSTEM (1 << 12)
|
98 |
|
|
#define IMAGE_FILE_DLL (1 << 13)
|
99 |
|
|
|
100 |
|
|
/* Desired characteristics (for validation). */
|
101 |
|
|
|
102 |
|
|
#define COFF_CHARACTERISTICS \
|
103 |
|
|
(IMAGE_FILE_32BIT_MACHINE)
|
104 |
|
|
|
105 |
|
|
/* Unwanted characteristics (for validation). */
|
106 |
|
|
|
107 |
|
|
#define COFF_NOT_CHARACTERISTICS \
|
108 |
|
|
(IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL)
|
109 |
|
|
|
110 |
|
|
/* Section flags. LTO emits byte-aligned read-only loadable data sections. */
|
111 |
|
|
|
112 |
|
|
#define IMAGE_SCN_CNT_INITIALIZED_DATA (1 << 6)
|
113 |
|
|
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA (1 << 7)
|
114 |
|
|
#define IMAGE_SCN_ALIGN_1BYTES (0x1 << 20)
|
115 |
|
|
#define IMAGE_SCN_MEM_DISCARDABLE (1 << 25)
|
116 |
|
|
#define IMAGE_SCN_MEM_SHARED (1 << 28)
|
117 |
|
|
#define IMAGE_SCN_MEM_READ (1 << 30)
|
118 |
|
|
|
119 |
|
|
#define COFF_SECTION_CHARACTERISTICS \
|
120 |
|
|
(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_ALIGN_1BYTES | \
|
121 |
|
|
IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_SHARED | IMAGE_SCN_MEM_READ)
|
122 |
|
|
|
123 |
|
|
/* Symbol-related constants. */
|
124 |
|
|
|
125 |
|
|
#define IMAGE_SYM_DEBUG (-2)
|
126 |
|
|
#define IMAGE_SYM_TYPE_NULL (0)
|
127 |
|
|
#define IMAGE_SYM_DTYPE_NULL (0)
|
128 |
|
|
#define IMAGE_SYM_CLASS_STATIC (3)
|
129 |
|
|
#define IMAGE_SYM_CLASS_FILE (103)
|
130 |
|
|
|
131 |
|
|
#define IMAGE_SYM_TYPE \
|
132 |
|
|
((IMAGE_SYM_DTYPE_NULL << 4) | IMAGE_SYM_TYPE_NULL)
|
133 |
|
|
|
134 |
|
|
/* Size of a COFF symbol in bytes. */
|
135 |
|
|
|
136 |
|
|
#define COFF_SYMBOL_SIZE (18)
|
137 |
|
|
|
138 |
|
|
/* On-disk file structures. */
|
139 |
|
|
|
140 |
|
|
struct Coff_header
|
141 |
|
|
{
|
142 |
|
|
unsigned char Machine[2];
|
143 |
|
|
unsigned char NumberOfSections[2];
|
144 |
|
|
unsigned char TimeDateStamp[4];
|
145 |
|
|
unsigned char PointerToSymbolTable[4];
|
146 |
|
|
unsigned char NumberOfSymbols[4];
|
147 |
|
|
unsigned char SizeOfOptionalHeader[2];
|
148 |
|
|
unsigned char Characteristics[2];
|
149 |
|
|
};
|
150 |
|
|
typedef struct Coff_header Coff_header;
|
151 |
|
|
|
152 |
|
|
struct Coff_section
|
153 |
|
|
{
|
154 |
|
|
unsigned char Name[8];
|
155 |
|
|
unsigned char VirtualSize[4];
|
156 |
|
|
unsigned char VirtualAddress[4];
|
157 |
|
|
unsigned char SizeOfRawData[4];
|
158 |
|
|
unsigned char PointerToRawData[4];
|
159 |
|
|
unsigned char PointerToRelocations[4];
|
160 |
|
|
unsigned char PointerToLinenumbers[4];
|
161 |
|
|
unsigned char NumberOfRelocations[2];
|
162 |
|
|
unsigned char NumberOfLinenumbers[2];
|
163 |
|
|
unsigned char Characteristics[4];
|
164 |
|
|
};
|
165 |
|
|
typedef struct Coff_section Coff_section;
|
166 |
|
|
|
167 |
|
|
struct Coff_symbol
|
168 |
|
|
{
|
169 |
|
|
unsigned char Name[8];
|
170 |
|
|
unsigned char Value[4];
|
171 |
|
|
unsigned char SectionNumber[2];
|
172 |
|
|
unsigned char Type[2];
|
173 |
|
|
unsigned char StorageClass[1];
|
174 |
|
|
unsigned char NumberOfAuxSymbols[1];
|
175 |
|
|
};
|
176 |
|
|
typedef struct Coff_symbol Coff_symbol;
|
177 |
|
|
|
178 |
|
|
struct Coff_aux_sym_file
|
179 |
|
|
{
|
180 |
|
|
unsigned char FileName[18];
|
181 |
|
|
};
|
182 |
|
|
typedef struct Coff_aux_sym_file Coff_aux_sym_file;
|
183 |
|
|
|
184 |
|
|
struct Coff_aux_sym_section
|
185 |
|
|
{
|
186 |
|
|
unsigned char Length[4];
|
187 |
|
|
unsigned char NumberOfRelocations[2];
|
188 |
|
|
unsigned char NumberOfLineNumbers[2];
|
189 |
|
|
unsigned char Checksum[4];
|
190 |
|
|
unsigned char Number[2];
|
191 |
|
|
unsigned char Selection[1];
|
192 |
|
|
unsigned char Unused[3];
|
193 |
|
|
};
|
194 |
|
|
typedef struct Coff_aux_sym_section Coff_aux_sym_section;
|
195 |
|
|
|
196 |
|
|
/* Accessor macros for the above structures. */
|
197 |
|
|
|
198 |
|
|
#define COFF_GET(struc,memb) \
|
199 |
|
|
((COFFENDIAN ? get_be : get_le) (&(struc)->memb[0], sizeof ((struc)->memb)))
|
200 |
|
|
|
201 |
|
|
#define COFF_PUT(struc,memb,val) \
|
202 |
|
|
((COFFENDIAN ? put_be : put_le) (&(struc)->memb[0], sizeof ((struc)->memb), val))
|
203 |
|
|
|
204 |
|
|
#define COFF_PUT_NDXSZ(struc,memb,val,ndx,sz) \
|
205 |
|
|
((COFFENDIAN ? put_be : put_le) (&(struc)->memb[ndx], sz, val))
|
206 |
|
|
|
207 |
|
|
/* In-memory file structures. */
|
208 |
|
|
|
209 |
|
|
/* Forward declared structs. */
|
210 |
|
|
|
211 |
|
|
struct lto_coff_data;
|
212 |
|
|
struct lto_coff_section;
|
213 |
|
|
struct lto_coff_file;
|
214 |
|
|
|
215 |
|
|
/* Section data in output files is made of these. */
|
216 |
|
|
|
217 |
|
|
struct lto_coff_data
|
218 |
|
|
{
|
219 |
|
|
/* Pointer to data block. */
|
220 |
|
|
void *d_buf;
|
221 |
|
|
|
222 |
|
|
/* Size of data block. */
|
223 |
|
|
ssize_t d_size;
|
224 |
|
|
|
225 |
|
|
/* Next data block for this section. */
|
226 |
|
|
struct lto_coff_data *next;
|
227 |
|
|
};
|
228 |
|
|
typedef struct lto_coff_data lto_coff_data;
|
229 |
|
|
|
230 |
|
|
/* This struct tracks the data for a section. */
|
231 |
|
|
|
232 |
|
|
struct lto_coff_section
|
233 |
|
|
{
|
234 |
|
|
/* Singly-linked list of section's data blocks. */
|
235 |
|
|
lto_coff_data *data_chain;
|
236 |
|
|
|
237 |
|
|
/* Offset in string table of name. */
|
238 |
|
|
size_t strtab_offs;
|
239 |
|
|
|
240 |
|
|
/* Section type: 0 = real, 1 = dummy. */
|
241 |
|
|
size_t type;
|
242 |
|
|
|
243 |
|
|
/* Section name. */
|
244 |
|
|
const char *name;
|
245 |
|
|
|
246 |
|
|
#if COFF_ALIGNMENT > 1
|
247 |
|
|
/* Number of trailing padding bytes needed. */
|
248 |
|
|
ssize_t pad_needed;
|
249 |
|
|
#endif
|
250 |
|
|
|
251 |
|
|
/* Raw section header data. */
|
252 |
|
|
Coff_section coffsec;
|
253 |
|
|
|
254 |
|
|
/* Next section for this file. */
|
255 |
|
|
struct lto_coff_section *next;
|
256 |
|
|
};
|
257 |
|
|
typedef struct lto_coff_section lto_coff_section;
|
258 |
|
|
|
259 |
|
|
/* A COFF file. */
|
260 |
|
|
|
261 |
|
|
struct lto_coff_file
|
262 |
|
|
{
|
263 |
|
|
/* The base information. */
|
264 |
|
|
lto_file base;
|
265 |
|
|
|
266 |
|
|
/* Common file members: */
|
267 |
|
|
|
268 |
|
|
/* The system file descriptor for the file. */
|
269 |
|
|
int fd;
|
270 |
|
|
|
271 |
|
|
/* The file's overall header. */
|
272 |
|
|
Coff_header coffhdr;
|
273 |
|
|
|
274 |
|
|
/* All sections in a singly-linked list. */
|
275 |
|
|
lto_coff_section *section_chain;
|
276 |
|
|
|
277 |
|
|
/* Readable file members: */
|
278 |
|
|
|
279 |
|
|
/* File total size. */
|
280 |
|
|
off_t file_size;
|
281 |
|
|
|
282 |
|
|
/* String table file offset, relative to base.offset. */
|
283 |
|
|
off_t strtab_offs;
|
284 |
|
|
|
285 |
|
|
/* Writable file members: */
|
286 |
|
|
|
287 |
|
|
/* The currently active section. */
|
288 |
|
|
lto_coff_section *scn;
|
289 |
|
|
|
290 |
|
|
/* The output stream for section header names. */
|
291 |
|
|
struct lto_output_stream *shstrtab_stream;
|
292 |
|
|
|
293 |
|
|
/* Linked list of data which must be freed *after* the file has been
|
294 |
|
|
closed. This is an annoying limitation of libelf. Which has been
|
295 |
|
|
faithfully reproduced here. */
|
296 |
|
|
struct lto_char_ptr_base *data;
|
297 |
|
|
};
|
298 |
|
|
typedef struct lto_coff_file lto_coff_file;
|
299 |
|
|
|
300 |
|
|
/* Data hunk iterator. */
|
301 |
|
|
|
302 |
|
|
#define COFF_FOR_ALL_DATA(sec,var) \
|
303 |
|
|
for (var = sec->data_chain; var; var = var->next)
|
304 |
|
|
|
305 |
|
|
/* Section list iterator. */
|
306 |
|
|
|
307 |
|
|
#define COFF_FOR_ALL_SECTIONS(file,var) \
|
308 |
|
|
for (var = file->section_chain; var; var = var->next)
|
309 |
|
|
|
310 |
|
|
/* Very simple endian-ness layer. */
|
311 |
|
|
|
312 |
|
|
#ifndef COFFENDIAN
|
313 |
|
|
#define COFFENDIAN (BYTES_BIG_ENDIAN)
|
314 |
|
|
#endif
|
315 |
|
|
|
316 |
|
|
static inline unsigned int
|
317 |
|
|
get_2_le (const unsigned char *ptr)
|
318 |
|
|
{
|
319 |
|
|
return ptr[0] | (ptr[1] << 8);
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
static inline unsigned int
|
323 |
|
|
get_4_le (const unsigned char *ptr)
|
324 |
|
|
{
|
325 |
|
|
return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
static inline unsigned int
|
329 |
|
|
get_2_be (const unsigned char *ptr)
|
330 |
|
|
{
|
331 |
|
|
return ptr[1] | (ptr[0] << 8);
|
332 |
|
|
}
|
333 |
|
|
|
334 |
|
|
static inline unsigned int
|
335 |
|
|
get_4_be (const unsigned char *ptr)
|
336 |
|
|
{
|
337 |
|
|
return ptr[3] | (ptr[2] << 8) | (ptr[1] << 16) | (ptr[0] << 24);
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
static inline unsigned int
|
341 |
|
|
get_be (const unsigned char *ptr, size_t size)
|
342 |
|
|
{
|
343 |
|
|
gcc_assert (size == 4 || size == 2);
|
344 |
|
|
return (size == 2) ? get_2_be (ptr) : get_4_be (ptr);
|
345 |
|
|
}
|
346 |
|
|
|
347 |
|
|
static inline unsigned int
|
348 |
|
|
get_le (const unsigned char *ptr, size_t size)
|
349 |
|
|
{
|
350 |
|
|
gcc_assert (size == 4 || size == 2);
|
351 |
|
|
return (size == 2) ? get_2_le (ptr) : get_4_le (ptr);
|
352 |
|
|
}
|
353 |
|
|
|
354 |
|
|
static inline void
|
355 |
|
|
put_2_le (unsigned char *ptr, unsigned int data)
|
356 |
|
|
{
|
357 |
|
|
ptr[0] = data & 0xff;
|
358 |
|
|
ptr[1] = (data >> 8) & 0xff;
|
359 |
|
|
}
|
360 |
|
|
|
361 |
|
|
static inline void
|
362 |
|
|
put_4_le (unsigned char *ptr, unsigned int data)
|
363 |
|
|
{
|
364 |
|
|
ptr[0] = data & 0xff;
|
365 |
|
|
ptr[1] = (data >> 8) & 0xff;
|
366 |
|
|
ptr[2] = (data >> 16) & 0xff;
|
367 |
|
|
ptr[3] = (data >> 24) & 0xff;
|
368 |
|
|
}
|
369 |
|
|
|
370 |
|
|
static inline void
|
371 |
|
|
put_2_be (unsigned char *ptr, unsigned int data)
|
372 |
|
|
{
|
373 |
|
|
ptr[1] = data & 0xff;
|
374 |
|
|
ptr[0] = (data >> 8) & 0xff;
|
375 |
|
|
}
|
376 |
|
|
|
377 |
|
|
static inline void
|
378 |
|
|
put_4_be (unsigned char *ptr, unsigned int data)
|
379 |
|
|
{
|
380 |
|
|
ptr[3] = data & 0xff;
|
381 |
|
|
ptr[2] = (data >> 8) & 0xff;
|
382 |
|
|
ptr[1] = (data >> 16) & 0xff;
|
383 |
|
|
ptr[0] = (data >> 24) & 0xff;
|
384 |
|
|
}
|
385 |
|
|
|
386 |
|
|
static inline void
|
387 |
|
|
put_le (unsigned char *ptr, size_t size, unsigned int data)
|
388 |
|
|
{
|
389 |
|
|
gcc_assert (size == 4 || size == 2);
|
390 |
|
|
(void) (size == 2 ? put_2_le : put_4_le) (ptr, data);
|
391 |
|
|
}
|
392 |
|
|
|
393 |
|
|
static inline void
|
394 |
|
|
put_be (unsigned char *ptr, size_t size, unsigned int data)
|
395 |
|
|
{
|
396 |
|
|
gcc_assert (size == 4 || size == 2);
|
397 |
|
|
(void) (size == 2 ? put_2_be : put_4_be) (ptr, data);
|
398 |
|
|
}
|
399 |
|
|
|
400 |
|
|
/* We use this for putting the string table size. */
|
401 |
|
|
|
402 |
|
|
#define COFF_PUT4(ptr, data) \
|
403 |
|
|
((COFFENDIAN ? put_4_be : put_4_le) (ptr, data))
|
404 |
|
|
|
405 |
|
|
|
406 |
|
|
#endif /* LTO_COFF_H */
|