annotate libiberty/simple-object-xcoff.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* simple-object-coff.c -- routines to manipulate XCOFF object files.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2013-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Written by Ian Lance Taylor, Google and David Edelsohn, IBM.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify it
kono
parents:
diff changeset
6 under the terms of the GNU General Public License as published by the
kono
parents:
diff changeset
7 Free Software Foundation; either version 2, or (at your option) any
kono
parents:
diff changeset
8 later version.
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
kono
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
13 GNU General Public License for more details.
kono
parents:
diff changeset
14
kono
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
16 along with this program; if not, write to the Free Software
kono
parents:
diff changeset
17 Foundation, 51 Franklin Street - Fifth Floor,
kono
parents:
diff changeset
18 Boston, MA 02110-1301, USA. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "config.h"
kono
parents:
diff changeset
21 #include "libiberty.h"
kono
parents:
diff changeset
22 #include "simple-object.h"
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 #include <errno.h>
kono
parents:
diff changeset
25 #include <stddef.h>
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 #ifdef HAVE_STDLIB_H
kono
parents:
diff changeset
28 #include <stdlib.h>
kono
parents:
diff changeset
29 #endif
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 #ifdef HAVE_STDINT_H
kono
parents:
diff changeset
32 #include <stdint.h>
kono
parents:
diff changeset
33 #endif
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 #ifdef HAVE_STRING_H
kono
parents:
diff changeset
36 #include <string.h>
kono
parents:
diff changeset
37 #endif
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 #ifdef HAVE_INTTYPES_H
kono
parents:
diff changeset
40 #include <inttypes.h>
kono
parents:
diff changeset
41 #endif
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 #include "simple-object-common.h"
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 /* XCOFF structures and constants. */
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 /* XCOFF file header. */
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 struct external_filehdr
kono
parents:
diff changeset
50 {
kono
parents:
diff changeset
51 unsigned char f_magic[2]; /* magic number */
kono
parents:
diff changeset
52 unsigned char f_nscns[2]; /* number of sections */
kono
parents:
diff changeset
53 unsigned char f_timdat[4]; /* time & date stamp */
kono
parents:
diff changeset
54 union
kono
parents:
diff changeset
55 {
kono
parents:
diff changeset
56 struct
kono
parents:
diff changeset
57 {
kono
parents:
diff changeset
58 unsigned char f_symptr[4]; /* file pointer to symtab */
kono
parents:
diff changeset
59 unsigned char f_nsyms[4]; /* number of symtab entries */
kono
parents:
diff changeset
60 unsigned char f_opthdr[2]; /* sizeof(optional hdr) */
kono
parents:
diff changeset
61 unsigned char f_flags[2]; /* flags */
kono
parents:
diff changeset
62 } xcoff32;
kono
parents:
diff changeset
63 struct
kono
parents:
diff changeset
64 {
kono
parents:
diff changeset
65 unsigned char f_symptr[8]; /* file pointer to symtab */
kono
parents:
diff changeset
66 unsigned char f_opthdr[2]; /* sizeof(optional hdr) */
kono
parents:
diff changeset
67 unsigned char f_flags[2]; /* flags */
kono
parents:
diff changeset
68 unsigned char f_nsyms[4]; /* number of symtab entries */
kono
parents:
diff changeset
69 } xcoff64;
kono
parents:
diff changeset
70 } u;
kono
parents:
diff changeset
71 };
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 /* Bits for filehdr f_flags field. */
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 #define F_EXEC (0x0002)
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 /* The known values of f_magic in an XCOFF file header. */
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 #define U802WRMAGIC 0730 /* Writeable text segments. */
kono
parents:
diff changeset
80 #define U802ROMAGIC 0735 /* Readonly sharable text segments. */
kono
parents:
diff changeset
81 #define U802TOCMAGIC 0737 /* Readonly text segments and TOC. */
kono
parents:
diff changeset
82 #define U803XTOCMAGIC 0757 /* Aix 4.3 64-bit XCOFF. */
kono
parents:
diff changeset
83 #define U64_TOCMAGIC 0767 /* AIX 5+ 64-bit XCOFF. */
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 /* XCOFF section header. */
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 struct external_scnhdr
kono
parents:
diff changeset
88 {
kono
parents:
diff changeset
89 unsigned char s_name[8]; /* section name */
kono
parents:
diff changeset
90 union
kono
parents:
diff changeset
91 {
kono
parents:
diff changeset
92 struct
kono
parents:
diff changeset
93 {
kono
parents:
diff changeset
94 unsigned char s_paddr[4]; /* physical address, aliased s_nlib */
kono
parents:
diff changeset
95 unsigned char s_vaddr[4]; /* virtual address */
kono
parents:
diff changeset
96 unsigned char s_size[4]; /* section size */
kono
parents:
diff changeset
97 unsigned char s_scnptr[4]; /* file ptr to raw data for section */
kono
parents:
diff changeset
98 unsigned char s_relptr[4]; /* file ptr to relocation */
kono
parents:
diff changeset
99 unsigned char s_lnnoptr[4]; /* file ptr to line numbers */
kono
parents:
diff changeset
100 unsigned char s_nreloc[2]; /* number of relocation entries */
kono
parents:
diff changeset
101 unsigned char s_nlnno[2]; /* number of line number entries */
kono
parents:
diff changeset
102 unsigned char s_flags[4]; /* flags */
kono
parents:
diff changeset
103 } xcoff32;
kono
parents:
diff changeset
104 struct
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 unsigned char s_paddr[8]; /* physical address, aliased s_nlib */
kono
parents:
diff changeset
107 unsigned char s_vaddr[8]; /* virtual address */
kono
parents:
diff changeset
108 unsigned char s_size[8]; /* section size */
kono
parents:
diff changeset
109 unsigned char s_scnptr[8]; /* file ptr to raw data for section */
kono
parents:
diff changeset
110 unsigned char s_relptr[8]; /* file ptr to relocation */
kono
parents:
diff changeset
111 unsigned char s_lnnoptr[8]; /* file ptr to line numbers */
kono
parents:
diff changeset
112 unsigned char s_nreloc[4]; /* number of relocation entries */
kono
parents:
diff changeset
113 unsigned char s_nlnno[4]; /* number of line number entries */
kono
parents:
diff changeset
114 unsigned char s_flags[4]; /* flags */
kono
parents:
diff changeset
115 } xcoff64;
kono
parents:
diff changeset
116 } u;
kono
parents:
diff changeset
117 };
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 #define SCNHSZ32 (40)
kono
parents:
diff changeset
120 #define SCNHSZ64 (68)
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 /* The length of the s_name field in struct external_scnhdr. */
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 #define SCNNMLEN (8)
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 /* Bits for scnhdr s_flags field. */
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 #define STYP_DATA 0x40
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 /* XCOFF symbol table entry. */
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 #define N_SYMNMLEN (8) /* # characters in a symbol name */
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 /* The format of an XCOFF symbol-table entry. */
kono
parents:
diff changeset
136 struct external_syment
kono
parents:
diff changeset
137 {
kono
parents:
diff changeset
138 union {
kono
parents:
diff changeset
139 struct {
kono
parents:
diff changeset
140 union {
kono
parents:
diff changeset
141 /* The name of the symbol. There is an implicit null character
kono
parents:
diff changeset
142 after the end of the array. */
kono
parents:
diff changeset
143 char n_name[N_SYMNMLEN];
kono
parents:
diff changeset
144 struct {
kono
parents:
diff changeset
145 /* If n_zeroes is zero, n_offset is the offset the name from
kono
parents:
diff changeset
146 the start of the string table. */
kono
parents:
diff changeset
147 unsigned char n_zeroes[4];
kono
parents:
diff changeset
148 unsigned char n_offset[4];
kono
parents:
diff changeset
149 } n;
kono
parents:
diff changeset
150 } n;
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 /* The symbol's value. */
kono
parents:
diff changeset
153 unsigned char n_value[4];
kono
parents:
diff changeset
154 } xcoff32;
kono
parents:
diff changeset
155 struct {
kono
parents:
diff changeset
156 /* The symbol's value. */
kono
parents:
diff changeset
157 unsigned char n_value[8];
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 /* The offset of the symbol from the start of the string table. */
kono
parents:
diff changeset
160 unsigned char n_offset[4];
kono
parents:
diff changeset
161 } xcoff64;
kono
parents:
diff changeset
162 } u;
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 /* The number of the section to which this symbol belongs. */
kono
parents:
diff changeset
165 unsigned char n_scnum[2];
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 /* The type of symbol. (It can be interpreted as an n_lang
kono
parents:
diff changeset
168 and an n_cpu byte, but we don't care about that here.) */
kono
parents:
diff changeset
169 unsigned char n_type[2];
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 /* The class of symbol (a C_* value). */
kono
parents:
diff changeset
172 unsigned char n_sclass[1];
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 /* The number of auxiliary symbols attached to this entry. */
kono
parents:
diff changeset
175 unsigned char n_numaux[1];
kono
parents:
diff changeset
176 };
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 #define SYMESZ (18)
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 /* Length allowed for filename in aux sym format 4. */
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 #define FILNMLEN (14)
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 /* Omits x_sym and other unused variants. */
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 union external_auxent
kono
parents:
diff changeset
187 {
kono
parents:
diff changeset
188 /* Aux sym format 4: file. */
kono
parents:
diff changeset
189 union
kono
parents:
diff changeset
190 {
kono
parents:
diff changeset
191 char x_fname[FILNMLEN];
kono
parents:
diff changeset
192 struct
kono
parents:
diff changeset
193 {
kono
parents:
diff changeset
194 unsigned char x_zeroes[4];
kono
parents:
diff changeset
195 unsigned char x_offset[4];
kono
parents:
diff changeset
196 unsigned char x_pad[FILNMLEN-8];
kono
parents:
diff changeset
197 unsigned char x_ftype;
kono
parents:
diff changeset
198 } _x;
kono
parents:
diff changeset
199 } x_file;
kono
parents:
diff changeset
200 /* Aux sym format 5: section. */
kono
parents:
diff changeset
201 struct
kono
parents:
diff changeset
202 {
kono
parents:
diff changeset
203 unsigned char x_scnlen[4]; /* section length */
kono
parents:
diff changeset
204 unsigned char x_nreloc[2]; /* # relocation entries */
kono
parents:
diff changeset
205 unsigned char x_nlinno[2]; /* # line numbers */
kono
parents:
diff changeset
206 } x_scn;
kono
parents:
diff changeset
207 /* CSECT auxiliary entry. */
kono
parents:
diff changeset
208 union
kono
parents:
diff changeset
209 {
kono
parents:
diff changeset
210 struct
kono
parents:
diff changeset
211 {
kono
parents:
diff changeset
212 struct
kono
parents:
diff changeset
213 {
kono
parents:
diff changeset
214 unsigned char x_scnlen[4]; /* csect length */
kono
parents:
diff changeset
215 unsigned char x_parmhash[4]; /* parm type hash index */
kono
parents:
diff changeset
216 unsigned char x_snhash[2]; /* sect num with parm hash */
kono
parents:
diff changeset
217 unsigned char x_smtyp; /* symbol align and type */
kono
parents:
diff changeset
218 unsigned char x_smclas; /* storage mapping class */
kono
parents:
diff changeset
219 unsigned char x_stab; /* dbx stab info index */
kono
parents:
diff changeset
220 unsigned char x_snstab[2]; /* sect num with dbx stab */
kono
parents:
diff changeset
221 } x_csect;
kono
parents:
diff changeset
222 } xcoff32;
kono
parents:
diff changeset
223 struct
kono
parents:
diff changeset
224 {
kono
parents:
diff changeset
225 struct
kono
parents:
diff changeset
226 {
kono
parents:
diff changeset
227 unsigned char x_scnlen_lo[4]; /* csect length */
kono
parents:
diff changeset
228 unsigned char x_parmhash[4]; /* parm type hash index */
kono
parents:
diff changeset
229 unsigned char x_snhash[2]; /* sect num with parm hash */
kono
parents:
diff changeset
230 unsigned char x_smtyp; /* symbol align and type */
kono
parents:
diff changeset
231 unsigned char x_smclas; /* storage mapping class */
kono
parents:
diff changeset
232 unsigned char x_scnlen_hi[4];
kono
parents:
diff changeset
233 unsigned char pad;
kono
parents:
diff changeset
234 unsigned char x_auxtype;
kono
parents:
diff changeset
235 } x_csect;
kono
parents:
diff changeset
236 } xcoff64;
kono
parents:
diff changeset
237 } u;
kono
parents:
diff changeset
238 /* SECTION/DWARF auxiliary entry. */
kono
parents:
diff changeset
239 struct
kono
parents:
diff changeset
240 {
kono
parents:
diff changeset
241 unsigned char x_scnlen[4]; /* section length */
kono
parents:
diff changeset
242 unsigned char pad1[4];
kono
parents:
diff changeset
243 unsigned char x_nreloc[4]; /* number RLDs */
kono
parents:
diff changeset
244 } x_sect;
kono
parents:
diff changeset
245 };
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 /* Symbol-related constants. */
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 #define N_DEBUG (-2)
kono
parents:
diff changeset
250 #define IMAGE_SYM_TYPE_NULL (0)
kono
parents:
diff changeset
251 #define IMAGE_SYM_DTYPE_NULL (0)
kono
parents:
diff changeset
252 #define IMAGE_SYM_CLASS_STATIC (3)
kono
parents:
diff changeset
253 #define IMAGE_SYM_CLASS_FILE (103)
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 #define IMAGE_SYM_TYPE \
kono
parents:
diff changeset
256 ((IMAGE_SYM_DTYPE_NULL << 4) | IMAGE_SYM_TYPE_NULL)
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 #define C_EXT (2)
kono
parents:
diff changeset
259 #define C_STAT (3)
kono
parents:
diff changeset
260 #define C_FILE (103)
kono
parents:
diff changeset
261 #define C_HIDEXT (107)
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 #define XTY_SD (1) /* section definition */
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 #define XMC_XO (7) /* extended operation */
kono
parents:
diff changeset
266
kono
parents:
diff changeset
267 /* Private data for an simple_object_read. */
kono
parents:
diff changeset
268
kono
parents:
diff changeset
269 struct simple_object_xcoff_read
kono
parents:
diff changeset
270 {
kono
parents:
diff changeset
271 /* Magic number. */
kono
parents:
diff changeset
272 unsigned short magic;
kono
parents:
diff changeset
273 /* Number of sections. */
kono
parents:
diff changeset
274 unsigned short nscns;
kono
parents:
diff changeset
275 /* File offset of symbol table. */
kono
parents:
diff changeset
276 off_t symptr;
kono
parents:
diff changeset
277 /* Number of symbol table entries. */
kono
parents:
diff changeset
278 unsigned int nsyms;
kono
parents:
diff changeset
279 /* Flags. */
kono
parents:
diff changeset
280 unsigned short flags;
kono
parents:
diff changeset
281 /* Offset of section headers in file. */
kono
parents:
diff changeset
282 off_t scnhdr_offset;
kono
parents:
diff changeset
283 };
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 /* Private data for an simple_object_attributes. */
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 struct simple_object_xcoff_attributes
kono
parents:
diff changeset
288 {
kono
parents:
diff changeset
289 /* Magic number. */
kono
parents:
diff changeset
290 unsigned short magic;
kono
parents:
diff changeset
291 /* Flags. */
kono
parents:
diff changeset
292 unsigned short flags;
kono
parents:
diff changeset
293 };
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 /* See if we have a XCOFF file. */
kono
parents:
diff changeset
296
kono
parents:
diff changeset
297 static void *
kono
parents:
diff changeset
298 simple_object_xcoff_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
kono
parents:
diff changeset
299 int descriptor, off_t offset,
kono
parents:
diff changeset
300 const char *segment_name ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
301 const char **errmsg, int *err)
kono
parents:
diff changeset
302 {
kono
parents:
diff changeset
303 unsigned short magic;
kono
parents:
diff changeset
304 unsigned short (*fetch_16) (const unsigned char *);
kono
parents:
diff changeset
305 unsigned int (*fetch_32) (const unsigned char *);
kono
parents:
diff changeset
306 ulong_type (*fetch_64) (const unsigned char *);
kono
parents:
diff changeset
307 unsigned char hdrbuf[sizeof (struct external_filehdr)];
kono
parents:
diff changeset
308 struct simple_object_xcoff_read *ocr;
kono
parents:
diff changeset
309 int u64;
kono
parents:
diff changeset
310
kono
parents:
diff changeset
311 magic = simple_object_fetch_big_16 (header);
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 if (magic != U802TOCMAGIC && magic != U64_TOCMAGIC)
kono
parents:
diff changeset
314 {
kono
parents:
diff changeset
315 *errmsg = NULL;
kono
parents:
diff changeset
316 *err = 0;
kono
parents:
diff changeset
317 return NULL;
kono
parents:
diff changeset
318 }
kono
parents:
diff changeset
319
kono
parents:
diff changeset
320 fetch_16 = simple_object_fetch_big_16;
kono
parents:
diff changeset
321 fetch_32 = simple_object_fetch_big_32;
kono
parents:
diff changeset
322 fetch_64 = simple_object_fetch_big_64;
kono
parents:
diff changeset
323
kono
parents:
diff changeset
324 if (!simple_object_internal_read (descriptor, offset, hdrbuf, sizeof hdrbuf,
kono
parents:
diff changeset
325 errmsg, err))
kono
parents:
diff changeset
326 return NULL;
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 u64 = magic == U64_TOCMAGIC;
kono
parents:
diff changeset
329
kono
parents:
diff changeset
330 ocr = XNEW (struct simple_object_xcoff_read);
kono
parents:
diff changeset
331 ocr->magic = magic;
kono
parents:
diff changeset
332 ocr->nscns = fetch_16 (hdrbuf + offsetof (struct external_filehdr, f_nscns));
kono
parents:
diff changeset
333 if (u64)
kono
parents:
diff changeset
334 {
kono
parents:
diff changeset
335 ocr->symptr = fetch_64 (hdrbuf
kono
parents:
diff changeset
336 + offsetof (struct external_filehdr,
kono
parents:
diff changeset
337 u.xcoff64.f_symptr));
kono
parents:
diff changeset
338 ocr->nsyms = fetch_32 (hdrbuf + offsetof (struct external_filehdr,
kono
parents:
diff changeset
339 u.xcoff64.f_nsyms));
kono
parents:
diff changeset
340 ocr->scnhdr_offset = (sizeof (struct external_filehdr)
kono
parents:
diff changeset
341 + fetch_16 (hdrbuf + offsetof (struct external_filehdr,
kono
parents:
diff changeset
342 u.xcoff64.f_opthdr)));
kono
parents:
diff changeset
343
kono
parents:
diff changeset
344 }
kono
parents:
diff changeset
345 else
kono
parents:
diff changeset
346 {
kono
parents:
diff changeset
347 ocr->symptr = fetch_32 (hdrbuf
kono
parents:
diff changeset
348 + offsetof (struct external_filehdr,
kono
parents:
diff changeset
349 u.xcoff32.f_symptr));
kono
parents:
diff changeset
350 ocr->nsyms = fetch_32 (hdrbuf + offsetof (struct external_filehdr,
kono
parents:
diff changeset
351 u.xcoff32.f_nsyms));
kono
parents:
diff changeset
352 ocr->scnhdr_offset = (sizeof (struct external_filehdr) - 4
kono
parents:
diff changeset
353 + fetch_16 (hdrbuf + offsetof (struct external_filehdr,
kono
parents:
diff changeset
354 u.xcoff32.f_opthdr)));
kono
parents:
diff changeset
355
kono
parents:
diff changeset
356 }
kono
parents:
diff changeset
357
kono
parents:
diff changeset
358 return (void *) ocr;
kono
parents:
diff changeset
359 }
kono
parents:
diff changeset
360
kono
parents:
diff changeset
361 /* Read the string table in a XCOFF file. */
kono
parents:
diff changeset
362
kono
parents:
diff changeset
363 static char *
kono
parents:
diff changeset
364 simple_object_xcoff_read_strtab (simple_object_read *sobj, size_t *strtab_size,
kono
parents:
diff changeset
365 const char **errmsg, int *err)
kono
parents:
diff changeset
366 {
kono
parents:
diff changeset
367 struct simple_object_xcoff_read *ocr =
kono
parents:
diff changeset
368 (struct simple_object_xcoff_read *) sobj->data;
kono
parents:
diff changeset
369 off_t strtab_offset;
kono
parents:
diff changeset
370 unsigned char strsizebuf[4];
kono
parents:
diff changeset
371 size_t strsize;
kono
parents:
diff changeset
372 char *strtab;
kono
parents:
diff changeset
373
kono
parents:
diff changeset
374 strtab_offset = sobj->offset + ocr->symptr
kono
parents:
diff changeset
375 + ocr->nsyms * SYMESZ;
kono
parents:
diff changeset
376 if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
kono
parents:
diff changeset
377 strsizebuf, 4, errmsg, err))
kono
parents:
diff changeset
378 return NULL;
kono
parents:
diff changeset
379 strsize = simple_object_fetch_big_32 (strsizebuf);
kono
parents:
diff changeset
380 strtab = XNEWVEC (char, strsize);
kono
parents:
diff changeset
381 if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
kono
parents:
diff changeset
382 (unsigned char *) strtab, strsize, errmsg,
kono
parents:
diff changeset
383 err))
kono
parents:
diff changeset
384 {
kono
parents:
diff changeset
385 XDELETEVEC (strtab);
kono
parents:
diff changeset
386 return NULL;
kono
parents:
diff changeset
387 }
kono
parents:
diff changeset
388 *strtab_size = strsize;
kono
parents:
diff changeset
389 return strtab;
kono
parents:
diff changeset
390 }
kono
parents:
diff changeset
391
kono
parents:
diff changeset
392 /* Find all sections in a XCOFF file. */
kono
parents:
diff changeset
393
kono
parents:
diff changeset
394 static const char *
kono
parents:
diff changeset
395 simple_object_xcoff_find_sections (simple_object_read *sobj,
kono
parents:
diff changeset
396 int (*pfn) (void *, const char *,
kono
parents:
diff changeset
397 off_t offset, off_t length),
kono
parents:
diff changeset
398 void *data,
kono
parents:
diff changeset
399 int *err)
kono
parents:
diff changeset
400 {
kono
parents:
diff changeset
401 struct simple_object_xcoff_read *ocr =
kono
parents:
diff changeset
402 (struct simple_object_xcoff_read *) sobj->data;
kono
parents:
diff changeset
403 int u64 = ocr->magic == U64_TOCMAGIC;
kono
parents:
diff changeset
404 size_t scnhdr_size;
kono
parents:
diff changeset
405 unsigned char *scnbuf;
kono
parents:
diff changeset
406 const char *errmsg;
kono
parents:
diff changeset
407 unsigned short (*fetch_16) (const unsigned char *);
kono
parents:
diff changeset
408 unsigned int (*fetch_32) (const unsigned char *);
kono
parents:
diff changeset
409 ulong_type (*fetch_64) (const unsigned char *);
kono
parents:
diff changeset
410 unsigned int nscns;
kono
parents:
diff changeset
411 char *strtab;
kono
parents:
diff changeset
412 size_t strtab_size;
kono
parents:
diff changeset
413 struct external_syment *symtab = NULL;
kono
parents:
diff changeset
414 unsigned int i;
kono
parents:
diff changeset
415
kono
parents:
diff changeset
416 scnhdr_size = u64 ? SCNHSZ64 : SCNHSZ32;
kono
parents:
diff changeset
417 scnbuf = XNEWVEC (unsigned char, scnhdr_size * ocr->nscns);
kono
parents:
diff changeset
418 if (!simple_object_internal_read (sobj->descriptor,
kono
parents:
diff changeset
419 sobj->offset + ocr->scnhdr_offset,
kono
parents:
diff changeset
420 scnbuf, scnhdr_size * ocr->nscns, &errmsg,
kono
parents:
diff changeset
421 err))
kono
parents:
diff changeset
422 {
kono
parents:
diff changeset
423 XDELETEVEC (scnbuf);
kono
parents:
diff changeset
424 return errmsg;
kono
parents:
diff changeset
425 }
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 fetch_16 = simple_object_fetch_big_16;
kono
parents:
diff changeset
428 fetch_32 = simple_object_fetch_big_32;
kono
parents:
diff changeset
429 fetch_64 = simple_object_fetch_big_64;
kono
parents:
diff changeset
430
kono
parents:
diff changeset
431 nscns = ocr->nscns;
kono
parents:
diff changeset
432 strtab = NULL;
kono
parents:
diff changeset
433 strtab_size = 0;
kono
parents:
diff changeset
434 for (i = 0; i < nscns; ++i)
kono
parents:
diff changeset
435 {
kono
parents:
diff changeset
436 unsigned char *scnhdr;
kono
parents:
diff changeset
437 unsigned char *scnname;
kono
parents:
diff changeset
438 char namebuf[SCNNMLEN + 1];
kono
parents:
diff changeset
439 char *name;
kono
parents:
diff changeset
440 off_t scnptr;
kono
parents:
diff changeset
441 off_t size;
kono
parents:
diff changeset
442
kono
parents:
diff changeset
443 scnhdr = scnbuf + i * scnhdr_size;
kono
parents:
diff changeset
444 scnname = scnhdr + offsetof (struct external_scnhdr, s_name);
kono
parents:
diff changeset
445 memcpy (namebuf, scnname, SCNNMLEN);
kono
parents:
diff changeset
446 namebuf[SCNNMLEN] = '\0';
kono
parents:
diff changeset
447 name = &namebuf[0];
kono
parents:
diff changeset
448 if (namebuf[0] == '/')
kono
parents:
diff changeset
449 {
kono
parents:
diff changeset
450 size_t strindex;
kono
parents:
diff changeset
451 char *end;
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 strindex = strtol (namebuf + 1, &end, 10);
kono
parents:
diff changeset
454 if (*end == '\0')
kono
parents:
diff changeset
455 {
kono
parents:
diff changeset
456 /* The real section name is found in the string
kono
parents:
diff changeset
457 table. */
kono
parents:
diff changeset
458 if (strtab == NULL)
kono
parents:
diff changeset
459 {
kono
parents:
diff changeset
460 strtab = simple_object_xcoff_read_strtab (sobj,
kono
parents:
diff changeset
461 &strtab_size,
kono
parents:
diff changeset
462 &errmsg, err);
kono
parents:
diff changeset
463 if (strtab == NULL)
kono
parents:
diff changeset
464 {
kono
parents:
diff changeset
465 XDELETEVEC (scnbuf);
kono
parents:
diff changeset
466 return errmsg;
kono
parents:
diff changeset
467 }
kono
parents:
diff changeset
468 }
kono
parents:
diff changeset
469
kono
parents:
diff changeset
470 if (strindex < 4 || strindex >= strtab_size)
kono
parents:
diff changeset
471 {
kono
parents:
diff changeset
472 XDELETEVEC (strtab);
kono
parents:
diff changeset
473 XDELETEVEC (scnbuf);
kono
parents:
diff changeset
474 *err = 0;
kono
parents:
diff changeset
475 return "section string index out of range";
kono
parents:
diff changeset
476 }
kono
parents:
diff changeset
477
kono
parents:
diff changeset
478 name = strtab + strindex;
kono
parents:
diff changeset
479 }
kono
parents:
diff changeset
480 }
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 if (u64)
kono
parents:
diff changeset
483 {
kono
parents:
diff changeset
484 scnptr = fetch_64 (scnhdr + offsetof (struct external_scnhdr,
kono
parents:
diff changeset
485 u.xcoff64.s_scnptr));
kono
parents:
diff changeset
486 size = fetch_64 (scnhdr + offsetof (struct external_scnhdr,
kono
parents:
diff changeset
487 u.xcoff64.s_size));
kono
parents:
diff changeset
488 }
kono
parents:
diff changeset
489 else
kono
parents:
diff changeset
490 {
kono
parents:
diff changeset
491 scnptr = fetch_32 (scnhdr + offsetof (struct external_scnhdr,
kono
parents:
diff changeset
492 u.xcoff32.s_scnptr));
kono
parents:
diff changeset
493 size = fetch_32 (scnhdr + offsetof (struct external_scnhdr,
kono
parents:
diff changeset
494 u.xcoff32.s_size));
kono
parents:
diff changeset
495 }
kono
parents:
diff changeset
496
kono
parents:
diff changeset
497 if (!(*pfn) (data, name, scnptr, size))
kono
parents:
diff changeset
498 break;
kono
parents:
diff changeset
499 }
kono
parents:
diff changeset
500
kono
parents:
diff changeset
501 /* Special handling for .go_export csect. */
kono
parents:
diff changeset
502 if (ocr->nsyms > 0)
kono
parents:
diff changeset
503 {
kono
parents:
diff changeset
504 unsigned char *sym;
kono
parents:
diff changeset
505 const char *n_name;
kono
parents:
diff changeset
506 off_t size, n_value;
kono
parents:
diff changeset
507 unsigned int n_numaux, n_offset, n_zeroes;
kono
parents:
diff changeset
508 short n_scnum;
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510 /* Read symbol table. */
kono
parents:
diff changeset
511 symtab = XNEWVEC (struct external_syment, ocr->nsyms * SYMESZ);
kono
parents:
diff changeset
512 if (!simple_object_internal_read (sobj->descriptor,
kono
parents:
diff changeset
513 sobj->offset + ocr->symptr,
kono
parents:
diff changeset
514 (unsigned char *) symtab,
kono
parents:
diff changeset
515 ocr->nsyms * SYMESZ,
kono
parents:
diff changeset
516 &errmsg, err))
kono
parents:
diff changeset
517 {
kono
parents:
diff changeset
518 XDELETEVEC (symtab);
kono
parents:
diff changeset
519 XDELETEVEC (scnbuf);
kono
parents:
diff changeset
520 return NULL;
kono
parents:
diff changeset
521 }
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 /* Search in symbol table if we have a ".go_export" symbol. */
kono
parents:
diff changeset
524 for (i = 0; i < ocr->nsyms; i += n_numaux + 1)
kono
parents:
diff changeset
525 {
kono
parents:
diff changeset
526 sym = (unsigned char *) &symtab[i];
kono
parents:
diff changeset
527 n_numaux = symtab[i].n_numaux[0];
kono
parents:
diff changeset
528
kono
parents:
diff changeset
529 if (symtab[i].n_sclass[0] != C_EXT
kono
parents:
diff changeset
530 && symtab[i].n_sclass[0] != C_HIDEXT)
kono
parents:
diff changeset
531 continue;
kono
parents:
diff changeset
532
kono
parents:
diff changeset
533 /* Must have at least one csect auxiliary entry. */
kono
parents:
diff changeset
534 if (n_numaux < 1 || i + n_numaux >= ocr->nsyms)
kono
parents:
diff changeset
535 continue;
kono
parents:
diff changeset
536
kono
parents:
diff changeset
537 n_scnum = fetch_16 (sym + offsetof (struct external_syment,
kono
parents:
diff changeset
538 n_scnum));
kono
parents:
diff changeset
539 if (n_scnum < 1 || (unsigned int) n_scnum > nscns)
kono
parents:
diff changeset
540 continue;
kono
parents:
diff changeset
541
kono
parents:
diff changeset
542 if (u64)
kono
parents:
diff changeset
543 {
kono
parents:
diff changeset
544 n_value = fetch_64 (sym + offsetof (struct external_syment,
kono
parents:
diff changeset
545 u.xcoff64.n_value));
kono
parents:
diff changeset
546 n_offset = fetch_32 (sym + offsetof (struct external_syment,
kono
parents:
diff changeset
547 u.xcoff64.n_offset));
kono
parents:
diff changeset
548 }
kono
parents:
diff changeset
549 else
kono
parents:
diff changeset
550 {
kono
parents:
diff changeset
551 /* ".go_export" is longer than N_SYMNMLEN. */
kono
parents:
diff changeset
552 n_zeroes = fetch_32 (sym + offsetof (struct external_syment,
kono
parents:
diff changeset
553 u.xcoff32.n.n.n_zeroes));
kono
parents:
diff changeset
554 if (n_zeroes != 0)
kono
parents:
diff changeset
555 continue;
kono
parents:
diff changeset
556
kono
parents:
diff changeset
557 n_value = fetch_32 (sym + offsetof (struct external_syment,
kono
parents:
diff changeset
558 u.xcoff32.n_value));
kono
parents:
diff changeset
559 n_offset = fetch_32 (sym + offsetof (struct external_syment,
kono
parents:
diff changeset
560 u.xcoff32.n.n.n_offset));
kono
parents:
diff changeset
561 }
kono
parents:
diff changeset
562
kono
parents:
diff changeset
563 /* The real symbol name is found in the string table. */
kono
parents:
diff changeset
564 if (strtab == NULL)
kono
parents:
diff changeset
565 {
kono
parents:
diff changeset
566 strtab = simple_object_xcoff_read_strtab (sobj,
kono
parents:
diff changeset
567 &strtab_size,
kono
parents:
diff changeset
568 &errmsg, err);
kono
parents:
diff changeset
569 if (strtab == NULL)
kono
parents:
diff changeset
570 {
kono
parents:
diff changeset
571 XDELETEVEC (symtab);
kono
parents:
diff changeset
572 XDELETEVEC (scnbuf);
kono
parents:
diff changeset
573 return errmsg;
kono
parents:
diff changeset
574 }
kono
parents:
diff changeset
575 }
kono
parents:
diff changeset
576
kono
parents:
diff changeset
577 if (n_offset >= strtab_size)
kono
parents:
diff changeset
578 {
kono
parents:
diff changeset
579 XDELETEVEC (strtab);
kono
parents:
diff changeset
580 XDELETEVEC (symtab);
kono
parents:
diff changeset
581 XDELETEVEC (scnbuf);
kono
parents:
diff changeset
582 *err = 0;
kono
parents:
diff changeset
583 return "symbol string index out of range";
kono
parents:
diff changeset
584 }
kono
parents:
diff changeset
585 n_name = strtab + n_offset;
kono
parents:
diff changeset
586
kono
parents:
diff changeset
587 if (!strcmp (n_name, ".go_export"))
kono
parents:
diff changeset
588 {
kono
parents:
diff changeset
589 union external_auxent *auxent;
kono
parents:
diff changeset
590 unsigned char *aux, *scnhdr;
kono
parents:
diff changeset
591 off_t scnptr, x_scnlen;
kono
parents:
diff changeset
592
kono
parents:
diff changeset
593 /* Found .go_export symbol, read its csect auxiliary entry.
kono
parents:
diff changeset
594 By convention, it is the last auxiliary entry. */
kono
parents:
diff changeset
595 auxent = (union external_auxent *) &symtab[i + n_numaux];
kono
parents:
diff changeset
596 aux = (unsigned char *) auxent;
kono
parents:
diff changeset
597 if (u64)
kono
parents:
diff changeset
598 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
599 /* Use an intermediate 64-bit type to avoid
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
600 compilation warning about 32-bit shift below on
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
601 hosts with 32-bit off_t which aren't supported by
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
602 AC_SYS_LARGEFILE. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
603 ulong_type x_scnlen64;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
604
111
kono
parents:
diff changeset
605 if ((auxent->u.xcoff64.x_csect.x_smtyp & 0x7) != XTY_SD
kono
parents:
diff changeset
606 || auxent->u.xcoff64.x_csect.x_smclas != XMC_XO)
kono
parents:
diff changeset
607 continue;
kono
parents:
diff changeset
608
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
609 x_scnlen64 =
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
610 fetch_32 (aux + offsetof (union external_auxent,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
611 u.xcoff64.x_csect.x_scnlen_hi));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
612 x_scnlen =
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
613 ((x_scnlen64 << 32)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
614 | fetch_32 (aux
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
615 + offsetof (union external_auxent,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
616 u.xcoff64.x_csect.x_scnlen_lo)));
111
kono
parents:
diff changeset
617 }
kono
parents:
diff changeset
618 else
kono
parents:
diff changeset
619 {
kono
parents:
diff changeset
620 if ((auxent->u.xcoff32.x_csect.x_smtyp & 0x7) != XTY_SD
kono
parents:
diff changeset
621 || auxent->u.xcoff32.x_csect.x_smclas != XMC_XO)
kono
parents:
diff changeset
622 continue;
kono
parents:
diff changeset
623
kono
parents:
diff changeset
624 x_scnlen = fetch_32 (aux + offsetof (union external_auxent,
kono
parents:
diff changeset
625 u.xcoff32.x_csect.x_scnlen));
kono
parents:
diff changeset
626 }
kono
parents:
diff changeset
627
kono
parents:
diff changeset
628 /* Get header of containing section. */
kono
parents:
diff changeset
629 scnhdr = scnbuf + (n_scnum - 1) * scnhdr_size;
kono
parents:
diff changeset
630 if (u64)
kono
parents:
diff changeset
631 {
kono
parents:
diff changeset
632 scnptr = fetch_64 (scnhdr + offsetof (struct external_scnhdr,
kono
parents:
diff changeset
633 u.xcoff64.s_scnptr));
kono
parents:
diff changeset
634 size = fetch_64 (scnhdr + offsetof (struct external_scnhdr,
kono
parents:
diff changeset
635 u.xcoff64.s_size));
kono
parents:
diff changeset
636 }
kono
parents:
diff changeset
637 else
kono
parents:
diff changeset
638 {
kono
parents:
diff changeset
639 scnptr = fetch_32 (scnhdr + offsetof (struct external_scnhdr,
kono
parents:
diff changeset
640 u.xcoff32.s_scnptr));
kono
parents:
diff changeset
641 size = fetch_32 (scnhdr + offsetof (struct external_scnhdr,
kono
parents:
diff changeset
642 u.xcoff32.s_size));
kono
parents:
diff changeset
643 }
kono
parents:
diff changeset
644 if (n_value + x_scnlen > size)
kono
parents:
diff changeset
645 break;
kono
parents:
diff changeset
646
kono
parents:
diff changeset
647 (*pfn) (data, ".go_export", scnptr + n_value, x_scnlen);
kono
parents:
diff changeset
648 break;
kono
parents:
diff changeset
649 }
kono
parents:
diff changeset
650 }
kono
parents:
diff changeset
651 }
kono
parents:
diff changeset
652
kono
parents:
diff changeset
653 if (symtab != NULL)
kono
parents:
diff changeset
654 XDELETEVEC (symtab);
kono
parents:
diff changeset
655 if (strtab != NULL)
kono
parents:
diff changeset
656 XDELETEVEC (strtab);
kono
parents:
diff changeset
657 XDELETEVEC (scnbuf);
kono
parents:
diff changeset
658
kono
parents:
diff changeset
659 return NULL;
kono
parents:
diff changeset
660 }
kono
parents:
diff changeset
661
kono
parents:
diff changeset
662 /* Fetch the attributes for an simple_object_read. */
kono
parents:
diff changeset
663
kono
parents:
diff changeset
664 static void *
kono
parents:
diff changeset
665 simple_object_xcoff_fetch_attributes (simple_object_read *sobj,
kono
parents:
diff changeset
666 const char **errmsg ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
667 int *err ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
668 {
kono
parents:
diff changeset
669 struct simple_object_xcoff_read *ocr =
kono
parents:
diff changeset
670 (struct simple_object_xcoff_read *) sobj->data;
kono
parents:
diff changeset
671 struct simple_object_xcoff_attributes *ret;
kono
parents:
diff changeset
672
kono
parents:
diff changeset
673 ret = XNEW (struct simple_object_xcoff_attributes);
kono
parents:
diff changeset
674 ret->magic = ocr->magic;
kono
parents:
diff changeset
675 ret->flags = ocr->flags;
kono
parents:
diff changeset
676 return ret;
kono
parents:
diff changeset
677 }
kono
parents:
diff changeset
678
kono
parents:
diff changeset
679 /* Release the private data for an simple_object_read. */
kono
parents:
diff changeset
680
kono
parents:
diff changeset
681 static void
kono
parents:
diff changeset
682 simple_object_xcoff_release_read (void *data)
kono
parents:
diff changeset
683 {
kono
parents:
diff changeset
684 XDELETE (data);
kono
parents:
diff changeset
685 }
kono
parents:
diff changeset
686
kono
parents:
diff changeset
687 /* Compare two attributes structures. */
kono
parents:
diff changeset
688
kono
parents:
diff changeset
689 static const char *
kono
parents:
diff changeset
690 simple_object_xcoff_attributes_merge (void *todata, void *fromdata, int *err)
kono
parents:
diff changeset
691 {
kono
parents:
diff changeset
692 struct simple_object_xcoff_attributes *to =
kono
parents:
diff changeset
693 (struct simple_object_xcoff_attributes *) todata;
kono
parents:
diff changeset
694 struct simple_object_xcoff_attributes *from =
kono
parents:
diff changeset
695 (struct simple_object_xcoff_attributes *) fromdata;
kono
parents:
diff changeset
696
kono
parents:
diff changeset
697 if (to->magic != from->magic)
kono
parents:
diff changeset
698 {
kono
parents:
diff changeset
699 *err = 0;
kono
parents:
diff changeset
700 return "XCOFF object format mismatch";
kono
parents:
diff changeset
701 }
kono
parents:
diff changeset
702 return NULL;
kono
parents:
diff changeset
703 }
kono
parents:
diff changeset
704
kono
parents:
diff changeset
705 /* Release the private data for an attributes structure. */
kono
parents:
diff changeset
706
kono
parents:
diff changeset
707 static void
kono
parents:
diff changeset
708 simple_object_xcoff_release_attributes (void *data)
kono
parents:
diff changeset
709 {
kono
parents:
diff changeset
710 XDELETE (data);
kono
parents:
diff changeset
711 }
kono
parents:
diff changeset
712
kono
parents:
diff changeset
713 /* Prepare to write out a file. */
kono
parents:
diff changeset
714
kono
parents:
diff changeset
715 static void *
kono
parents:
diff changeset
716 simple_object_xcoff_start_write (void *attributes_data,
kono
parents:
diff changeset
717 const char **errmsg ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
718 int *err ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
719 {
kono
parents:
diff changeset
720 struct simple_object_xcoff_attributes *attrs =
kono
parents:
diff changeset
721 (struct simple_object_xcoff_attributes *) attributes_data;
kono
parents:
diff changeset
722 struct simple_object_xcoff_attributes *ret;
kono
parents:
diff changeset
723
kono
parents:
diff changeset
724 /* We're just going to record the attributes, but we need to make a
kono
parents:
diff changeset
725 copy because the user may delete them. */
kono
parents:
diff changeset
726 ret = XNEW (struct simple_object_xcoff_attributes);
kono
parents:
diff changeset
727 *ret = *attrs;
kono
parents:
diff changeset
728 return ret;
kono
parents:
diff changeset
729 }
kono
parents:
diff changeset
730
kono
parents:
diff changeset
731 /* Write out a XCOFF filehdr. */
kono
parents:
diff changeset
732
kono
parents:
diff changeset
733 static int
kono
parents:
diff changeset
734 simple_object_xcoff_write_filehdr (simple_object_write *sobj, int descriptor,
kono
parents:
diff changeset
735 unsigned int nscns, size_t symtab_offset,
kono
parents:
diff changeset
736 unsigned int nsyms, const char **errmsg,
kono
parents:
diff changeset
737 int *err)
kono
parents:
diff changeset
738 {
kono
parents:
diff changeset
739 struct simple_object_xcoff_attributes *attrs =
kono
parents:
diff changeset
740 (struct simple_object_xcoff_attributes *) sobj->data;
kono
parents:
diff changeset
741 int u64 = attrs->magic == U64_TOCMAGIC;
kono
parents:
diff changeset
742 unsigned char hdrbuf[sizeof (struct external_filehdr)];
kono
parents:
diff changeset
743 unsigned char *hdr;
kono
parents:
diff changeset
744 void (*set_16) (unsigned char *, unsigned short);
kono
parents:
diff changeset
745 void (*set_32) (unsigned char *, unsigned int);
kono
parents:
diff changeset
746 void (*set_64) (unsigned char *, ulong_type);
kono
parents:
diff changeset
747
kono
parents:
diff changeset
748 hdr = &hdrbuf[0];
kono
parents:
diff changeset
749
kono
parents:
diff changeset
750 set_16 = simple_object_set_big_16;
kono
parents:
diff changeset
751 set_32 = simple_object_set_big_32;
kono
parents:
diff changeset
752 set_64 = simple_object_set_big_64;
kono
parents:
diff changeset
753
kono
parents:
diff changeset
754 memset (hdr, 0, sizeof (struct external_filehdr));
kono
parents:
diff changeset
755
kono
parents:
diff changeset
756 set_16 (hdr + offsetof (struct external_filehdr, f_magic), attrs->magic);
kono
parents:
diff changeset
757 set_16 (hdr + offsetof (struct external_filehdr, f_nscns), nscns);
kono
parents:
diff changeset
758 /* f_timdat left as zero. */
kono
parents:
diff changeset
759 if (u64)
kono
parents:
diff changeset
760 {
kono
parents:
diff changeset
761 set_64 (hdr + offsetof (struct external_filehdr, u.xcoff64.f_symptr),
kono
parents:
diff changeset
762 symtab_offset);
kono
parents:
diff changeset
763 set_32 (hdr + offsetof (struct external_filehdr, u.xcoff64.f_nsyms),
kono
parents:
diff changeset
764 nsyms);
kono
parents:
diff changeset
765 /* f_opthdr left as zero. */
kono
parents:
diff changeset
766 set_16 (hdr + offsetof (struct external_filehdr, u.xcoff64.f_flags),
kono
parents:
diff changeset
767 attrs->flags);
kono
parents:
diff changeset
768 }
kono
parents:
diff changeset
769 else
kono
parents:
diff changeset
770 {
kono
parents:
diff changeset
771 set_32 (hdr + offsetof (struct external_filehdr, u.xcoff64.f_symptr),
kono
parents:
diff changeset
772 symtab_offset);
kono
parents:
diff changeset
773 set_32 (hdr + offsetof (struct external_filehdr, u.xcoff64.f_nsyms),
kono
parents:
diff changeset
774 nsyms);
kono
parents:
diff changeset
775 /* f_opthdr left as zero. */
kono
parents:
diff changeset
776 set_16 (hdr + offsetof (struct external_filehdr, u.xcoff64.f_flags),
kono
parents:
diff changeset
777 attrs->flags);
kono
parents:
diff changeset
778 }
kono
parents:
diff changeset
779
kono
parents:
diff changeset
780 return simple_object_internal_write (descriptor, 0, hdrbuf,
kono
parents:
diff changeset
781 sizeof (struct external_filehdr),
kono
parents:
diff changeset
782 errmsg, err);
kono
parents:
diff changeset
783 }
kono
parents:
diff changeset
784
kono
parents:
diff changeset
785 /* Write out a XCOFF section header. */
kono
parents:
diff changeset
786
kono
parents:
diff changeset
787 static int
kono
parents:
diff changeset
788 simple_object_xcoff_write_scnhdr (simple_object_write *sobj,
kono
parents:
diff changeset
789 int descriptor,
kono
parents:
diff changeset
790 const char *name, size_t *name_offset,
kono
parents:
diff changeset
791 off_t scnhdr_offset, size_t scnsize,
kono
parents:
diff changeset
792 off_t offset, unsigned int align,
kono
parents:
diff changeset
793 const char **errmsg, int *err)
kono
parents:
diff changeset
794 {
kono
parents:
diff changeset
795 struct simple_object_xcoff_read *ocr =
kono
parents:
diff changeset
796 (struct simple_object_xcoff_read *) sobj->data;
kono
parents:
diff changeset
797 int u64 = ocr->magic == U64_TOCMAGIC;
kono
parents:
diff changeset
798 void (*set_32) (unsigned char *, unsigned int);
kono
parents:
diff changeset
799 void (*set_64) (unsigned char *, unsigned int);
kono
parents:
diff changeset
800 unsigned char hdrbuf[sizeof (struct external_scnhdr)];
kono
parents:
diff changeset
801 unsigned char *hdr;
kono
parents:
diff changeset
802 size_t namelen;
kono
parents:
diff changeset
803 unsigned int flags;
kono
parents:
diff changeset
804
kono
parents:
diff changeset
805 set_32 = simple_object_set_big_32;
kono
parents:
diff changeset
806 set_64 = simple_object_set_big_32;
kono
parents:
diff changeset
807
kono
parents:
diff changeset
808 memset (hdrbuf, 0, sizeof hdrbuf);
kono
parents:
diff changeset
809 hdr = &hdrbuf[0];
kono
parents:
diff changeset
810
kono
parents:
diff changeset
811 namelen = strlen (name);
kono
parents:
diff changeset
812 if (namelen <= SCNNMLEN)
kono
parents:
diff changeset
813 strncpy ((char *) hdr + offsetof (struct external_scnhdr, s_name),
kono
parents:
diff changeset
814 name, SCNNMLEN);
kono
parents:
diff changeset
815 else
kono
parents:
diff changeset
816 {
kono
parents:
diff changeset
817 snprintf ((char *) hdr + offsetof (struct external_scnhdr, s_name),
kono
parents:
diff changeset
818 SCNNMLEN, "/%lu", (unsigned long) *name_offset);
kono
parents:
diff changeset
819 *name_offset += namelen + 1;
kono
parents:
diff changeset
820 }
kono
parents:
diff changeset
821
kono
parents:
diff changeset
822 /* s_paddr left as zero. */
kono
parents:
diff changeset
823 /* s_vaddr left as zero. */
kono
parents:
diff changeset
824 if (u64)
kono
parents:
diff changeset
825 {
kono
parents:
diff changeset
826 set_64 (hdr + offsetof (struct external_scnhdr, u.xcoff64.s_size),
kono
parents:
diff changeset
827 scnsize);
kono
parents:
diff changeset
828 set_64 (hdr + offsetof (struct external_scnhdr, u.xcoff64.s_scnptr),
kono
parents:
diff changeset
829 offset);
kono
parents:
diff changeset
830 }
kono
parents:
diff changeset
831 else
kono
parents:
diff changeset
832 {
kono
parents:
diff changeset
833 set_32 (hdr + offsetof (struct external_scnhdr, u.xcoff32.s_size),
kono
parents:
diff changeset
834 scnsize);
kono
parents:
diff changeset
835 set_32 (hdr + offsetof (struct external_scnhdr, u.xcoff32.s_scnptr),
kono
parents:
diff changeset
836 offset);
kono
parents:
diff changeset
837 }
kono
parents:
diff changeset
838 /* s_relptr left as zero. */
kono
parents:
diff changeset
839 /* s_lnnoptr left as zero. */
kono
parents:
diff changeset
840 /* s_nreloc left as zero. */
kono
parents:
diff changeset
841 /* s_nlnno left as zero. */
kono
parents:
diff changeset
842 flags = STYP_DATA;
kono
parents:
diff changeset
843 if (align > 13)
kono
parents:
diff changeset
844 align = 13;
kono
parents:
diff changeset
845 if (u64)
kono
parents:
diff changeset
846 set_32 (hdr + offsetof (struct external_scnhdr, u.xcoff64.s_flags), flags);
kono
parents:
diff changeset
847 else
kono
parents:
diff changeset
848 set_32 (hdr + offsetof (struct external_scnhdr, u.xcoff32.s_flags), flags);
kono
parents:
diff changeset
849
kono
parents:
diff changeset
850 return simple_object_internal_write (descriptor, scnhdr_offset, hdrbuf,
kono
parents:
diff changeset
851 u64 ? SCNHSZ64 : SCNHSZ32,
kono
parents:
diff changeset
852 errmsg, err);
kono
parents:
diff changeset
853 }
kono
parents:
diff changeset
854
kono
parents:
diff changeset
855 /* Write out a complete XCOFF file. */
kono
parents:
diff changeset
856
kono
parents:
diff changeset
857 static const char *
kono
parents:
diff changeset
858 simple_object_xcoff_write_to_file (simple_object_write *sobj, int descriptor,
kono
parents:
diff changeset
859 int *err)
kono
parents:
diff changeset
860 {
kono
parents:
diff changeset
861 struct simple_object_xcoff_read *ocr =
kono
parents:
diff changeset
862 (struct simple_object_xcoff_read *) sobj->data;
kono
parents:
diff changeset
863 int u64 = ocr->magic == U64_TOCMAGIC;
kono
parents:
diff changeset
864 unsigned int nscns, secnum;
kono
parents:
diff changeset
865 simple_object_write_section *section;
kono
parents:
diff changeset
866 off_t scnhdr_offset;
kono
parents:
diff changeset
867 size_t symtab_offset;
kono
parents:
diff changeset
868 off_t secsym_offset;
kono
parents:
diff changeset
869 unsigned int nsyms;
kono
parents:
diff changeset
870 size_t offset;
kono
parents:
diff changeset
871 size_t name_offset;
kono
parents:
diff changeset
872 const char *errmsg;
kono
parents:
diff changeset
873 unsigned char strsizebuf[4];
kono
parents:
diff changeset
874 /* The interface doesn't give us access to the name of the input file
kono
parents:
diff changeset
875 yet. We want to use its basename for the FILE symbol. This is
kono
parents:
diff changeset
876 what 'gas' uses when told to assemble from stdin. */
kono
parents:
diff changeset
877 const char *source_filename = "fake";
kono
parents:
diff changeset
878 size_t sflen;
kono
parents:
diff changeset
879 union
kono
parents:
diff changeset
880 {
kono
parents:
diff changeset
881 struct external_syment sym;
kono
parents:
diff changeset
882 union external_auxent aux;
kono
parents:
diff changeset
883 } syms[2];
kono
parents:
diff changeset
884 void (*set_16) (unsigned char *, unsigned short);
kono
parents:
diff changeset
885 void (*set_32) (unsigned char *, unsigned int);
kono
parents:
diff changeset
886
kono
parents:
diff changeset
887 set_16 = simple_object_set_big_16;
kono
parents:
diff changeset
888 set_32 = simple_object_set_big_32;
kono
parents:
diff changeset
889
kono
parents:
diff changeset
890 nscns = 0;
kono
parents:
diff changeset
891 for (section = sobj->sections; section != NULL; section = section->next)
kono
parents:
diff changeset
892 ++nscns;
kono
parents:
diff changeset
893
kono
parents:
diff changeset
894 scnhdr_offset = sizeof (struct external_filehdr) - (u64 ? 4 : 0);
kono
parents:
diff changeset
895 offset = scnhdr_offset + nscns * (u64 ? SCNHSZ64 : SCNHSZ32);
kono
parents:
diff changeset
896 name_offset = 4;
kono
parents:
diff changeset
897 for (section = sobj->sections; section != NULL; section = section->next)
kono
parents:
diff changeset
898 {
kono
parents:
diff changeset
899 size_t mask;
kono
parents:
diff changeset
900 size_t new_offset;
kono
parents:
diff changeset
901 size_t scnsize;
kono
parents:
diff changeset
902 struct simple_object_write_section_buffer *buffer;
kono
parents:
diff changeset
903
kono
parents:
diff changeset
904 mask = (1U << section->align) - 1;
kono
parents:
diff changeset
905 new_offset = offset & mask;
kono
parents:
diff changeset
906 new_offset &= ~ mask;
kono
parents:
diff changeset
907 while (new_offset > offset)
kono
parents:
diff changeset
908 {
kono
parents:
diff changeset
909 unsigned char zeroes[16];
kono
parents:
diff changeset
910 size_t write;
kono
parents:
diff changeset
911
kono
parents:
diff changeset
912 memset (zeroes, 0, sizeof zeroes);
kono
parents:
diff changeset
913 write = new_offset - offset;
kono
parents:
diff changeset
914 if (write > sizeof zeroes)
kono
parents:
diff changeset
915 write = sizeof zeroes;
kono
parents:
diff changeset
916 if (!simple_object_internal_write (descriptor, offset, zeroes, write,
kono
parents:
diff changeset
917 &errmsg, err))
kono
parents:
diff changeset
918 return errmsg;
kono
parents:
diff changeset
919 }
kono
parents:
diff changeset
920
kono
parents:
diff changeset
921 scnsize = 0;
kono
parents:
diff changeset
922 for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
kono
parents:
diff changeset
923 {
kono
parents:
diff changeset
924 if (!simple_object_internal_write (descriptor, offset + scnsize,
kono
parents:
diff changeset
925 ((const unsigned char *)
kono
parents:
diff changeset
926 buffer->buffer),
kono
parents:
diff changeset
927 buffer->size, &errmsg, err))
kono
parents:
diff changeset
928 return errmsg;
kono
parents:
diff changeset
929 scnsize += buffer->size;
kono
parents:
diff changeset
930 }
kono
parents:
diff changeset
931
kono
parents:
diff changeset
932 if (!simple_object_xcoff_write_scnhdr (sobj, descriptor, section->name,
kono
parents:
diff changeset
933 &name_offset, scnhdr_offset,
kono
parents:
diff changeset
934 scnsize, offset, section->align,
kono
parents:
diff changeset
935 &errmsg, err))
kono
parents:
diff changeset
936 return errmsg;
kono
parents:
diff changeset
937
kono
parents:
diff changeset
938 scnhdr_offset += u64 ? SCNHSZ64 : SCNHSZ32;
kono
parents:
diff changeset
939 offset += scnsize;
kono
parents:
diff changeset
940 }
kono
parents:
diff changeset
941
kono
parents:
diff changeset
942 /* Symbol table is always half-word aligned. */
kono
parents:
diff changeset
943 offset += (offset & 1);
kono
parents:
diff changeset
944 /* There is a file symbol and a section symbol per section,
kono
parents:
diff changeset
945 and each of these has a single auxiliary symbol following. */
kono
parents:
diff changeset
946 nsyms = 2 * (nscns + 1);
kono
parents:
diff changeset
947 symtab_offset = offset;
kono
parents:
diff changeset
948 /* Advance across space reserved for symbol table to locate
kono
parents:
diff changeset
949 start of string table. */
kono
parents:
diff changeset
950 offset += nsyms * SYMESZ;
kono
parents:
diff changeset
951
kono
parents:
diff changeset
952 /* Write out file symbol. */
kono
parents:
diff changeset
953 memset (&syms[0], 0, sizeof (syms));
kono
parents:
diff changeset
954 if (!u64)
kono
parents:
diff changeset
955 strcpy ((char *)&syms[0].sym.u.xcoff32.n.n_name[0], ".file");
kono
parents:
diff changeset
956 set_16 (&syms[0].sym.n_scnum[0], N_DEBUG);
kono
parents:
diff changeset
957 set_16 (&syms[0].sym.n_type[0], IMAGE_SYM_TYPE);
kono
parents:
diff changeset
958 syms[0].sym.n_sclass[0] = C_FILE;
kono
parents:
diff changeset
959 syms[0].sym.n_numaux[0] = 1;
kono
parents:
diff changeset
960 /* The name need not be nul-terminated if it fits into the x_fname field
kono
parents:
diff changeset
961 directly, but must be if it has to be placed into the string table. */
kono
parents:
diff changeset
962 sflen = strlen (source_filename);
kono
parents:
diff changeset
963 if (sflen <= FILNMLEN)
kono
parents:
diff changeset
964 memcpy (&syms[1].aux.x_file.x_fname[0], source_filename, sflen);
kono
parents:
diff changeset
965 else
kono
parents:
diff changeset
966 {
kono
parents:
diff changeset
967 set_32 (&syms[1].aux.x_file._x.x_offset[0], name_offset);
kono
parents:
diff changeset
968 if (!simple_object_internal_write (descriptor, offset + name_offset,
kono
parents:
diff changeset
969 ((const unsigned char *)
kono
parents:
diff changeset
970 source_filename),
kono
parents:
diff changeset
971 sflen + 1, &errmsg, err))
kono
parents:
diff changeset
972 return errmsg;
kono
parents:
diff changeset
973 name_offset += strlen (source_filename) + 1;
kono
parents:
diff changeset
974 }
kono
parents:
diff changeset
975 if (!simple_object_internal_write (descriptor, symtab_offset,
kono
parents:
diff changeset
976 (const unsigned char *) &syms[0],
kono
parents:
diff changeset
977 sizeof (syms), &errmsg, err))
kono
parents:
diff changeset
978 return errmsg;
kono
parents:
diff changeset
979
kono
parents:
diff changeset
980 /* Write the string table length, followed by the strings and section
kono
parents:
diff changeset
981 symbols in step with each other. */
kono
parents:
diff changeset
982 set_32 (strsizebuf, name_offset);
kono
parents:
diff changeset
983 if (!simple_object_internal_write (descriptor, offset, strsizebuf, 4,
kono
parents:
diff changeset
984 &errmsg, err))
kono
parents:
diff changeset
985 return errmsg;
kono
parents:
diff changeset
986
kono
parents:
diff changeset
987 name_offset = 4;
kono
parents:
diff changeset
988 secsym_offset = symtab_offset + sizeof (syms);
kono
parents:
diff changeset
989 memset (&syms[0], 0, sizeof (syms));
kono
parents:
diff changeset
990 set_16 (&syms[0].sym.n_type[0], IMAGE_SYM_TYPE);
kono
parents:
diff changeset
991 syms[0].sym.n_sclass[0] = C_STAT;
kono
parents:
diff changeset
992 syms[0].sym.n_numaux[0] = 1;
kono
parents:
diff changeset
993 secnum = 1;
kono
parents:
diff changeset
994
kono
parents:
diff changeset
995 for (section = sobj->sections; section != NULL; section = section->next)
kono
parents:
diff changeset
996 {
kono
parents:
diff changeset
997 size_t namelen;
kono
parents:
diff changeset
998 size_t scnsize;
kono
parents:
diff changeset
999 struct simple_object_write_section_buffer *buffer;
kono
parents:
diff changeset
1000
kono
parents:
diff changeset
1001 namelen = strlen (section->name);
kono
parents:
diff changeset
1002 set_16 (&syms[0].sym.n_scnum[0], secnum++);
kono
parents:
diff changeset
1003 scnsize = 0;
kono
parents:
diff changeset
1004 for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
kono
parents:
diff changeset
1005 scnsize += buffer->size;
kono
parents:
diff changeset
1006 set_32 (&syms[1].aux.x_scn.x_scnlen[0], scnsize);
kono
parents:
diff changeset
1007 if (namelen > SCNNMLEN)
kono
parents:
diff changeset
1008 {
kono
parents:
diff changeset
1009 set_32 (&syms[0].sym.u.xcoff32.n.n.n_zeroes[0], 0);
kono
parents:
diff changeset
1010 set_32 (&syms[0].sym.u.xcoff32.n.n.n_offset[0], name_offset);
kono
parents:
diff changeset
1011 if (!simple_object_internal_write (descriptor, offset + name_offset,
kono
parents:
diff changeset
1012 ((const unsigned char *)
kono
parents:
diff changeset
1013 section->name),
kono
parents:
diff changeset
1014 namelen + 1, &errmsg, err))
kono
parents:
diff changeset
1015 return errmsg;
kono
parents:
diff changeset
1016 name_offset += namelen + 1;
kono
parents:
diff changeset
1017 }
kono
parents:
diff changeset
1018 else
kono
parents:
diff changeset
1019 {
kono
parents:
diff changeset
1020 memcpy (&syms[0].sym.u.xcoff32.n.n_name[0], section->name,
kono
parents:
diff changeset
1021 strlen (section->name));
kono
parents:
diff changeset
1022 memset (&syms[0].sym.u.xcoff32.n.n_name[strlen (section->name)], 0,
kono
parents:
diff changeset
1023 N_SYMNMLEN - strlen (section->name));
kono
parents:
diff changeset
1024 }
kono
parents:
diff changeset
1025
kono
parents:
diff changeset
1026 if (!simple_object_internal_write (descriptor, secsym_offset,
kono
parents:
diff changeset
1027 (const unsigned char *) &syms[0],
kono
parents:
diff changeset
1028 sizeof (syms), &errmsg, err))
kono
parents:
diff changeset
1029 return errmsg;
kono
parents:
diff changeset
1030 secsym_offset += sizeof (syms);
kono
parents:
diff changeset
1031 }
kono
parents:
diff changeset
1032
kono
parents:
diff changeset
1033 if (!simple_object_xcoff_write_filehdr (sobj, descriptor, nscns,
kono
parents:
diff changeset
1034 symtab_offset, nsyms, &errmsg, err))
kono
parents:
diff changeset
1035 return errmsg;
kono
parents:
diff changeset
1036
kono
parents:
diff changeset
1037 return NULL;
kono
parents:
diff changeset
1038 }
kono
parents:
diff changeset
1039
kono
parents:
diff changeset
1040 /* Release the private data for an simple_object_write structure. */
kono
parents:
diff changeset
1041
kono
parents:
diff changeset
1042 static void
kono
parents:
diff changeset
1043 simple_object_xcoff_release_write (void *data)
kono
parents:
diff changeset
1044 {
kono
parents:
diff changeset
1045 XDELETE (data);
kono
parents:
diff changeset
1046 }
kono
parents:
diff changeset
1047
kono
parents:
diff changeset
1048 /* The XCOFF functions. */
kono
parents:
diff changeset
1049
kono
parents:
diff changeset
1050 const struct simple_object_functions simple_object_xcoff_functions =
kono
parents:
diff changeset
1051 {
kono
parents:
diff changeset
1052 simple_object_xcoff_match,
kono
parents:
diff changeset
1053 simple_object_xcoff_find_sections,
kono
parents:
diff changeset
1054 simple_object_xcoff_fetch_attributes,
kono
parents:
diff changeset
1055 simple_object_xcoff_release_read,
kono
parents:
diff changeset
1056 simple_object_xcoff_attributes_merge,
kono
parents:
diff changeset
1057 simple_object_xcoff_release_attributes,
kono
parents:
diff changeset
1058 simple_object_xcoff_start_write,
kono
parents:
diff changeset
1059 simple_object_xcoff_write_to_file,
kono
parents:
diff changeset
1060 simple_object_xcoff_release_write,
kono
parents:
diff changeset
1061 NULL
kono
parents:
diff changeset
1062 };