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