xref: /dflybsd-src/contrib/binutils-2.27/libiberty/simple-object-coff.c (revision e656dc90e3d65d744d534af2f5ea88cf8101ebcf)
1*a9fa9459Szrj /* simple-object-coff.c -- routines to manipulate COFF object files.
2*a9fa9459Szrj    Copyright 2010 Free Software Foundation, Inc.
3*a9fa9459Szrj    Written by Ian Lance Taylor, Google.
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 /* COFF structures and constants.  */
46*a9fa9459Szrj 
47*a9fa9459Szrj /* COFF 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   unsigned char f_symptr[4];	/* file pointer to symtab	*/
55*a9fa9459Szrj   unsigned char f_nsyms[4];	/* number of symtab entries	*/
56*a9fa9459Szrj   unsigned char f_opthdr[2];	/* sizeof(optional hdr)		*/
57*a9fa9459Szrj   unsigned char f_flags[2];	/* flags			*/
58*a9fa9459Szrj };
59*a9fa9459Szrj 
60*a9fa9459Szrj /* Bits for filehdr f_flags field.  */
61*a9fa9459Szrj 
62*a9fa9459Szrj #define F_EXEC			(0x0002)
63*a9fa9459Szrj #define IMAGE_FILE_SYSTEM	(0x1000)
64*a9fa9459Szrj #define IMAGE_FILE_DLL		(0x2000)
65*a9fa9459Szrj 
66*a9fa9459Szrj /* COFF section header.  */
67*a9fa9459Szrj 
68*a9fa9459Szrj struct external_scnhdr
69*a9fa9459Szrj {
70*a9fa9459Szrj   unsigned char s_name[8];	/* section name				*/
71*a9fa9459Szrj   unsigned char s_paddr[4];	/* physical address, aliased s_nlib 	*/
72*a9fa9459Szrj   unsigned char s_vaddr[4];	/* virtual address			*/
73*a9fa9459Szrj   unsigned char s_size[4];	/* section size				*/
74*a9fa9459Szrj   unsigned char s_scnptr[4];	/* file ptr to raw data for section 	*/
75*a9fa9459Szrj   unsigned char s_relptr[4];	/* file ptr to relocation		*/
76*a9fa9459Szrj   unsigned char s_lnnoptr[4];	/* file ptr to line numbers		*/
77*a9fa9459Szrj   unsigned char s_nreloc[2];	/* number of relocation entries		*/
78*a9fa9459Szrj   unsigned char s_nlnno[2];	/* number of line number entries	*/
79*a9fa9459Szrj   unsigned char s_flags[4];	/* flags				*/
80*a9fa9459Szrj };
81*a9fa9459Szrj 
82*a9fa9459Szrj /* The length of the s_name field in struct external_scnhdr.  */
83*a9fa9459Szrj 
84*a9fa9459Szrj #define SCNNMLEN (8)
85*a9fa9459Szrj 
86*a9fa9459Szrj /* Bits for scnhdr s_flags field.  This includes some bits defined
87*a9fa9459Szrj    only for PE.  This may need to be moved into coff_magic.  */
88*a9fa9459Szrj 
89*a9fa9459Szrj #define STYP_DATA			(1 << 6)
90*a9fa9459Szrj #define IMAGE_SCN_MEM_DISCARDABLE	(1 << 25)
91*a9fa9459Szrj #define IMAGE_SCN_MEM_SHARED		(1 << 28)
92*a9fa9459Szrj #define IMAGE_SCN_MEM_READ		(1 << 30)
93*a9fa9459Szrj 
94*a9fa9459Szrj #define IMAGE_SCN_ALIGN_POWER_BIT_POS	     20
95*a9fa9459Szrj #define IMAGE_SCN_ALIGN_POWER_CONST(val)     \
96*a9fa9459Szrj   (((val) + 1) << IMAGE_SCN_ALIGN_POWER_BIT_POS)
97*a9fa9459Szrj 
98*a9fa9459Szrj /* COFF symbol table entry.  */
99*a9fa9459Szrj 
100*a9fa9459Szrj #define E_SYMNMLEN	8	/* # characters in a symbol name	*/
101*a9fa9459Szrj 
102*a9fa9459Szrj struct external_syment
103*a9fa9459Szrj {
104*a9fa9459Szrj   union
105*a9fa9459Szrj   {
106*a9fa9459Szrj     unsigned char e_name[E_SYMNMLEN];
107*a9fa9459Szrj 
108*a9fa9459Szrj     struct
109*a9fa9459Szrj     {
110*a9fa9459Szrj       unsigned char e_zeroes[4];
111*a9fa9459Szrj       unsigned char e_offset[4];
112*a9fa9459Szrj     } e;
113*a9fa9459Szrj   } e;
114*a9fa9459Szrj 
115*a9fa9459Szrj   unsigned char e_value[4];
116*a9fa9459Szrj   unsigned char e_scnum[2];
117*a9fa9459Szrj   unsigned char e_type[2];
118*a9fa9459Szrj   unsigned char e_sclass[1];
119*a9fa9459Szrj   unsigned char e_numaux[1];
120*a9fa9459Szrj };
121*a9fa9459Szrj 
122*a9fa9459Szrj /* Length allowed for filename in aux sym format 4.  */
123*a9fa9459Szrj 
124*a9fa9459Szrj #define E_FILNMLEN	18
125*a9fa9459Szrj 
126*a9fa9459Szrj /* Omits x_sym and other unused variants.  */
127*a9fa9459Szrj 
128*a9fa9459Szrj union external_auxent
129*a9fa9459Szrj {
130*a9fa9459Szrj   /* Aux sym format 4: file.  */
131*a9fa9459Szrj   union
132*a9fa9459Szrj   {
133*a9fa9459Szrj     char x_fname[E_FILNMLEN];
134*a9fa9459Szrj     struct
135*a9fa9459Szrj     {
136*a9fa9459Szrj       unsigned char x_zeroes[4];
137*a9fa9459Szrj       unsigned char x_offset[4];
138*a9fa9459Szrj     } x_n;
139*a9fa9459Szrj   } x_file;
140*a9fa9459Szrj   /* Aux sym format 5: section.  */
141*a9fa9459Szrj   struct
142*a9fa9459Szrj   {
143*a9fa9459Szrj     unsigned char x_scnlen[4];		/* section length		*/
144*a9fa9459Szrj     unsigned char x_nreloc[2];		/* # relocation entries		*/
145*a9fa9459Szrj     unsigned char x_nlinno[2];		/* # line numbers		*/
146*a9fa9459Szrj     unsigned char x_checksum[4];	/* section COMDAT checksum	*/
147*a9fa9459Szrj     unsigned char x_associated[2];	/* COMDAT assoc section index	*/
148*a9fa9459Szrj     unsigned char x_comdat[1];		/* COMDAT selection number	*/
149*a9fa9459Szrj   } x_scn;
150*a9fa9459Szrj };
151*a9fa9459Szrj 
152*a9fa9459Szrj /* Symbol-related constants.  */
153*a9fa9459Szrj 
154*a9fa9459Szrj #define IMAGE_SYM_DEBUG		(-2)
155*a9fa9459Szrj #define IMAGE_SYM_TYPE_NULL	(0)
156*a9fa9459Szrj #define IMAGE_SYM_DTYPE_NULL	(0)
157*a9fa9459Szrj #define IMAGE_SYM_CLASS_STATIC	(3)
158*a9fa9459Szrj #define IMAGE_SYM_CLASS_FILE	(103)
159*a9fa9459Szrj 
160*a9fa9459Szrj #define IMAGE_SYM_TYPE \
161*a9fa9459Szrj   ((IMAGE_SYM_DTYPE_NULL << 4) | IMAGE_SYM_TYPE_NULL)
162*a9fa9459Szrj 
163*a9fa9459Szrj /* Private data for an simple_object_read.  */
164*a9fa9459Szrj 
165*a9fa9459Szrj struct simple_object_coff_read
166*a9fa9459Szrj {
167*a9fa9459Szrj   /* Magic number.  */
168*a9fa9459Szrj   unsigned short magic;
169*a9fa9459Szrj   /* Whether the file is big-endian.  */
170*a9fa9459Szrj   unsigned char is_big_endian;
171*a9fa9459Szrj   /* Number of sections.  */
172*a9fa9459Szrj   unsigned short nscns;
173*a9fa9459Szrj   /* File offset of symbol table.  */
174*a9fa9459Szrj   off_t symptr;
175*a9fa9459Szrj   /* Number of symbol table entries.  */
176*a9fa9459Szrj   unsigned int nsyms;
177*a9fa9459Szrj   /* Flags.  */
178*a9fa9459Szrj   unsigned short flags;
179*a9fa9459Szrj   /* Offset of section headers in file.  */
180*a9fa9459Szrj   off_t scnhdr_offset;
181*a9fa9459Szrj };
182*a9fa9459Szrj 
183*a9fa9459Szrj /* Private data for an simple_object_attributes.  */
184*a9fa9459Szrj 
185*a9fa9459Szrj struct simple_object_coff_attributes
186*a9fa9459Szrj {
187*a9fa9459Szrj   /* Magic number.  */
188*a9fa9459Szrj   unsigned short magic;
189*a9fa9459Szrj   /* Whether the file is big-endian.  */
190*a9fa9459Szrj   unsigned char is_big_endian;
191*a9fa9459Szrj   /* Flags.  */
192*a9fa9459Szrj   unsigned short flags;
193*a9fa9459Szrj };
194*a9fa9459Szrj 
195*a9fa9459Szrj /* There is no magic number which indicates a COFF file as opposed to
196*a9fa9459Szrj    any other sort of file.  Instead, each COFF file starts with a
197*a9fa9459Szrj    two-byte magic number which also indicates the type of the target.
198*a9fa9459Szrj    This struct holds a magic number as well as characteristics of that
199*a9fa9459Szrj    COFF format.  */
200*a9fa9459Szrj 
201*a9fa9459Szrj struct coff_magic_struct
202*a9fa9459Szrj {
203*a9fa9459Szrj   /* Magic number.  */
204*a9fa9459Szrj   unsigned short magic;
205*a9fa9459Szrj   /* Whether this magic number is for a big-endian file.  */
206*a9fa9459Szrj   unsigned char is_big_endian;
207*a9fa9459Szrj   /* Flag bits, in the f_flags fields, which indicates that this file
208*a9fa9459Szrj      is not a relocatable object file.  There is no flag which
209*a9fa9459Szrj      specifically indicates a relocatable object file, it is only
210*a9fa9459Szrj      implied by the absence of these flags.  */
211*a9fa9459Szrj   unsigned short non_object_flags;
212*a9fa9459Szrj };
213*a9fa9459Szrj 
214*a9fa9459Szrj /* This is a list of the COFF magic numbers which we recognize, namely
215*a9fa9459Szrj    the ones used on Windows.  More can be added as needed.  */
216*a9fa9459Szrj 
217*a9fa9459Szrj static const struct coff_magic_struct coff_magic[] =
218*a9fa9459Szrj {
219*a9fa9459Szrj   /* i386.  */
220*a9fa9459Szrj   { 0x14c, 0, F_EXEC | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL },
221*a9fa9459Szrj   /* x86_64.  */
222*a9fa9459Szrj   { 0x8664, 0, F_EXEC | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL }
223*a9fa9459Szrj };
224*a9fa9459Szrj 
225*a9fa9459Szrj /* See if we have a COFF file.  */
226*a9fa9459Szrj 
227*a9fa9459Szrj static void *
simple_object_coff_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)228*a9fa9459Szrj simple_object_coff_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
229*a9fa9459Szrj 			  int descriptor, off_t offset,
230*a9fa9459Szrj 			  const char *segment_name ATTRIBUTE_UNUSED,
231*a9fa9459Szrj 			  const char **errmsg, int *err)
232*a9fa9459Szrj {
233*a9fa9459Szrj   size_t c;
234*a9fa9459Szrj   unsigned short magic_big;
235*a9fa9459Szrj   unsigned short magic_little;
236*a9fa9459Szrj   unsigned short magic;
237*a9fa9459Szrj   size_t i;
238*a9fa9459Szrj   int is_big_endian;
239*a9fa9459Szrj   unsigned short (*fetch_16) (const unsigned char *);
240*a9fa9459Szrj   unsigned int (*fetch_32) (const unsigned char *);
241*a9fa9459Szrj   unsigned char hdrbuf[sizeof (struct external_filehdr)];
242*a9fa9459Szrj   unsigned short flags;
243*a9fa9459Szrj   struct simple_object_coff_read *ocr;
244*a9fa9459Szrj 
245*a9fa9459Szrj   c = sizeof (coff_magic) / sizeof (coff_magic[0]);
246*a9fa9459Szrj   magic_big = simple_object_fetch_big_16 (header);
247*a9fa9459Szrj   magic_little = simple_object_fetch_little_16 (header);
248*a9fa9459Szrj   for (i = 0; i < c; ++i)
249*a9fa9459Szrj     {
250*a9fa9459Szrj       if (coff_magic[i].is_big_endian
251*a9fa9459Szrj 	  ? coff_magic[i].magic == magic_big
252*a9fa9459Szrj 	  : coff_magic[i].magic == magic_little)
253*a9fa9459Szrj 	break;
254*a9fa9459Szrj     }
255*a9fa9459Szrj   if (i >= c)
256*a9fa9459Szrj     {
257*a9fa9459Szrj       *errmsg = NULL;
258*a9fa9459Szrj       *err = 0;
259*a9fa9459Szrj       return NULL;
260*a9fa9459Szrj     }
261*a9fa9459Szrj   is_big_endian = coff_magic[i].is_big_endian;
262*a9fa9459Szrj 
263*a9fa9459Szrj   magic = is_big_endian ? magic_big : magic_little;
264*a9fa9459Szrj   fetch_16 = (is_big_endian
265*a9fa9459Szrj 	      ? simple_object_fetch_big_16
266*a9fa9459Szrj 	      : simple_object_fetch_little_16);
267*a9fa9459Szrj   fetch_32 = (is_big_endian
268*a9fa9459Szrj 	      ? simple_object_fetch_big_32
269*a9fa9459Szrj 	      : simple_object_fetch_little_32);
270*a9fa9459Szrj 
271*a9fa9459Szrj   if (!simple_object_internal_read (descriptor, offset, hdrbuf, sizeof hdrbuf,
272*a9fa9459Szrj 				    errmsg, err))
273*a9fa9459Szrj     return NULL;
274*a9fa9459Szrj 
275*a9fa9459Szrj   flags = fetch_16 (hdrbuf + offsetof (struct external_filehdr, f_flags));
276*a9fa9459Szrj   if ((flags & coff_magic[i].non_object_flags) != 0)
277*a9fa9459Szrj     {
278*a9fa9459Szrj       *errmsg = "not relocatable object file";
279*a9fa9459Szrj       *err = 0;
280*a9fa9459Szrj       return NULL;
281*a9fa9459Szrj     }
282*a9fa9459Szrj 
283*a9fa9459Szrj   ocr = XNEW (struct simple_object_coff_read);
284*a9fa9459Szrj   ocr->magic = magic;
285*a9fa9459Szrj   ocr->is_big_endian = is_big_endian;
286*a9fa9459Szrj   ocr->nscns = fetch_16 (hdrbuf + offsetof (struct external_filehdr, f_nscns));
287*a9fa9459Szrj   ocr->symptr = fetch_32 (hdrbuf
288*a9fa9459Szrj 			  + offsetof (struct external_filehdr, f_symptr));
289*a9fa9459Szrj   ocr->nsyms = fetch_32 (hdrbuf + offsetof (struct external_filehdr, f_nsyms));
290*a9fa9459Szrj   ocr->flags = flags;
291*a9fa9459Szrj   ocr->scnhdr_offset = (sizeof (struct external_filehdr)
292*a9fa9459Szrj 			+ fetch_16 (hdrbuf + offsetof (struct external_filehdr,
293*a9fa9459Szrj 						       f_opthdr)));
294*a9fa9459Szrj 
295*a9fa9459Szrj   return (void *) ocr;
296*a9fa9459Szrj }
297*a9fa9459Szrj 
298*a9fa9459Szrj /* Read the string table in a COFF file.  */
299*a9fa9459Szrj 
300*a9fa9459Szrj static char *
simple_object_coff_read_strtab(simple_object_read * sobj,size_t * strtab_size,const char ** errmsg,int * err)301*a9fa9459Szrj simple_object_coff_read_strtab (simple_object_read *sobj, size_t *strtab_size,
302*a9fa9459Szrj 				const char **errmsg, int *err)
303*a9fa9459Szrj {
304*a9fa9459Szrj   struct simple_object_coff_read *ocr =
305*a9fa9459Szrj     (struct simple_object_coff_read *) sobj->data;
306*a9fa9459Szrj   off_t strtab_offset;
307*a9fa9459Szrj   unsigned char strsizebuf[4];
308*a9fa9459Szrj   size_t strsize;
309*a9fa9459Szrj   char *strtab;
310*a9fa9459Szrj 
311*a9fa9459Szrj   strtab_offset = sobj->offset + ocr->symptr
312*a9fa9459Szrj 		  + ocr->nsyms * sizeof (struct external_syment);
313*a9fa9459Szrj   if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
314*a9fa9459Szrj 				    strsizebuf, 4, errmsg, err))
315*a9fa9459Szrj     return NULL;
316*a9fa9459Szrj   strsize = (ocr->is_big_endian
317*a9fa9459Szrj 	     ? simple_object_fetch_big_32 (strsizebuf)
318*a9fa9459Szrj 	     : simple_object_fetch_little_32 (strsizebuf));
319*a9fa9459Szrj   strtab = XNEWVEC (char, strsize);
320*a9fa9459Szrj   if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
321*a9fa9459Szrj 				    (unsigned char *) strtab, strsize, errmsg,
322*a9fa9459Szrj 				    err))
323*a9fa9459Szrj     {
324*a9fa9459Szrj       XDELETEVEC (strtab);
325*a9fa9459Szrj       return NULL;
326*a9fa9459Szrj     }
327*a9fa9459Szrj   *strtab_size = strsize;
328*a9fa9459Szrj   return strtab;
329*a9fa9459Szrj }
330*a9fa9459Szrj 
331*a9fa9459Szrj /* Find all sections in a COFF file.  */
332*a9fa9459Szrj 
333*a9fa9459Szrj static const char *
simple_object_coff_find_sections(simple_object_read * sobj,int (* pfn)(void *,const char *,off_t offset,off_t length),void * data,int * err)334*a9fa9459Szrj simple_object_coff_find_sections (simple_object_read *sobj,
335*a9fa9459Szrj 				  int (*pfn) (void *, const char *,
336*a9fa9459Szrj 					      off_t offset, off_t length),
337*a9fa9459Szrj 				  void *data,
338*a9fa9459Szrj 				  int *err)
339*a9fa9459Szrj {
340*a9fa9459Szrj   struct simple_object_coff_read *ocr =
341*a9fa9459Szrj     (struct simple_object_coff_read *) sobj->data;
342*a9fa9459Szrj   size_t scnhdr_size;
343*a9fa9459Szrj   unsigned char *scnbuf;
344*a9fa9459Szrj   const char *errmsg;
345*a9fa9459Szrj   unsigned int (*fetch_32) (const unsigned char *);
346*a9fa9459Szrj   unsigned int nscns;
347*a9fa9459Szrj   char *strtab;
348*a9fa9459Szrj   size_t strtab_size;
349*a9fa9459Szrj   unsigned int i;
350*a9fa9459Szrj 
351*a9fa9459Szrj   scnhdr_size = sizeof (struct external_scnhdr);
352*a9fa9459Szrj   scnbuf = XNEWVEC (unsigned char, scnhdr_size * ocr->nscns);
353*a9fa9459Szrj   if (!simple_object_internal_read (sobj->descriptor,
354*a9fa9459Szrj 				    sobj->offset + ocr->scnhdr_offset,
355*a9fa9459Szrj 				    scnbuf, scnhdr_size * ocr->nscns, &errmsg,
356*a9fa9459Szrj 				    err))
357*a9fa9459Szrj     {
358*a9fa9459Szrj       XDELETEVEC (scnbuf);
359*a9fa9459Szrj       return errmsg;
360*a9fa9459Szrj     }
361*a9fa9459Szrj 
362*a9fa9459Szrj   fetch_32 = (ocr->is_big_endian
363*a9fa9459Szrj 	      ? simple_object_fetch_big_32
364*a9fa9459Szrj 	      : simple_object_fetch_little_32);
365*a9fa9459Szrj 
366*a9fa9459Szrj   nscns = ocr->nscns;
367*a9fa9459Szrj   strtab = NULL;
368*a9fa9459Szrj   strtab_size = 0;
369*a9fa9459Szrj   for (i = 0; i < nscns; ++i)
370*a9fa9459Szrj     {
371*a9fa9459Szrj       unsigned char *scnhdr;
372*a9fa9459Szrj       unsigned char *scnname;
373*a9fa9459Szrj       char namebuf[SCNNMLEN + 1];
374*a9fa9459Szrj       char *name;
375*a9fa9459Szrj       off_t scnptr;
376*a9fa9459Szrj       unsigned int size;
377*a9fa9459Szrj 
378*a9fa9459Szrj       scnhdr = scnbuf + i * scnhdr_size;
379*a9fa9459Szrj       scnname = scnhdr + offsetof (struct external_scnhdr, s_name);
380*a9fa9459Szrj       memcpy (namebuf, scnname, SCNNMLEN);
381*a9fa9459Szrj       namebuf[SCNNMLEN] = '\0';
382*a9fa9459Szrj       name = &namebuf[0];
383*a9fa9459Szrj       if (namebuf[0] == '/')
384*a9fa9459Szrj 	{
385*a9fa9459Szrj 	  size_t strindex;
386*a9fa9459Szrj 	  char *end;
387*a9fa9459Szrj 
388*a9fa9459Szrj 	  strindex = strtol (namebuf + 1, &end, 10);
389*a9fa9459Szrj 	  if (*end == '\0')
390*a9fa9459Szrj 	    {
391*a9fa9459Szrj 	      /* The real section name is found in the string
392*a9fa9459Szrj 		 table.  */
393*a9fa9459Szrj 	      if (strtab == NULL)
394*a9fa9459Szrj 		{
395*a9fa9459Szrj 		  strtab = simple_object_coff_read_strtab (sobj,
396*a9fa9459Szrj 							   &strtab_size,
397*a9fa9459Szrj 							   &errmsg, err);
398*a9fa9459Szrj 		  if (strtab == NULL)
399*a9fa9459Szrj 		    {
400*a9fa9459Szrj 		      XDELETEVEC (scnbuf);
401*a9fa9459Szrj 		      return errmsg;
402*a9fa9459Szrj 		    }
403*a9fa9459Szrj 		}
404*a9fa9459Szrj 
405*a9fa9459Szrj 	      if (strindex < 4 || strindex >= strtab_size)
406*a9fa9459Szrj 		{
407*a9fa9459Szrj 		  XDELETEVEC (strtab);
408*a9fa9459Szrj 		  XDELETEVEC (scnbuf);
409*a9fa9459Szrj 		  *err = 0;
410*a9fa9459Szrj 		  return "section string index out of range";
411*a9fa9459Szrj 		}
412*a9fa9459Szrj 
413*a9fa9459Szrj 	      name = strtab + strindex;
414*a9fa9459Szrj 	    }
415*a9fa9459Szrj 	}
416*a9fa9459Szrj 
417*a9fa9459Szrj       scnptr = fetch_32 (scnhdr + offsetof (struct external_scnhdr, s_scnptr));
418*a9fa9459Szrj       size = fetch_32 (scnhdr + offsetof (struct external_scnhdr, s_size));
419*a9fa9459Szrj 
420*a9fa9459Szrj       if (!(*pfn) (data, name, scnptr, size))
421*a9fa9459Szrj 	break;
422*a9fa9459Szrj     }
423*a9fa9459Szrj 
424*a9fa9459Szrj   if (strtab != NULL)
425*a9fa9459Szrj     XDELETEVEC (strtab);
426*a9fa9459Szrj   XDELETEVEC (scnbuf);
427*a9fa9459Szrj 
428*a9fa9459Szrj   return NULL;
429*a9fa9459Szrj }
430*a9fa9459Szrj 
431*a9fa9459Szrj /* Fetch the attributes for an simple_object_read.  */
432*a9fa9459Szrj 
433*a9fa9459Szrj static void *
simple_object_coff_fetch_attributes(simple_object_read * sobj,const char ** errmsg ATTRIBUTE_UNUSED,int * err ATTRIBUTE_UNUSED)434*a9fa9459Szrj simple_object_coff_fetch_attributes (simple_object_read *sobj,
435*a9fa9459Szrj 				     const char **errmsg ATTRIBUTE_UNUSED,
436*a9fa9459Szrj 				     int *err ATTRIBUTE_UNUSED)
437*a9fa9459Szrj {
438*a9fa9459Szrj   struct simple_object_coff_read *ocr =
439*a9fa9459Szrj     (struct simple_object_coff_read *) sobj->data;
440*a9fa9459Szrj   struct simple_object_coff_attributes *ret;
441*a9fa9459Szrj 
442*a9fa9459Szrj   ret = XNEW (struct simple_object_coff_attributes);
443*a9fa9459Szrj   ret->magic = ocr->magic;
444*a9fa9459Szrj   ret->is_big_endian = ocr->is_big_endian;
445*a9fa9459Szrj   ret->flags = ocr->flags;
446*a9fa9459Szrj   return ret;
447*a9fa9459Szrj }
448*a9fa9459Szrj 
449*a9fa9459Szrj /* Release the private data for an simple_object_read.  */
450*a9fa9459Szrj 
451*a9fa9459Szrj static void
simple_object_coff_release_read(void * data)452*a9fa9459Szrj simple_object_coff_release_read (void *data)
453*a9fa9459Szrj {
454*a9fa9459Szrj   XDELETE (data);
455*a9fa9459Szrj }
456*a9fa9459Szrj 
457*a9fa9459Szrj /* Compare two attributes structures.  */
458*a9fa9459Szrj 
459*a9fa9459Szrj static const char *
simple_object_coff_attributes_merge(void * todata,void * fromdata,int * err)460*a9fa9459Szrj simple_object_coff_attributes_merge (void *todata, void *fromdata, int *err)
461*a9fa9459Szrj {
462*a9fa9459Szrj   struct simple_object_coff_attributes *to =
463*a9fa9459Szrj     (struct simple_object_coff_attributes *) todata;
464*a9fa9459Szrj   struct simple_object_coff_attributes *from =
465*a9fa9459Szrj     (struct simple_object_coff_attributes *) fromdata;
466*a9fa9459Szrj 
467*a9fa9459Szrj   if (to->magic != from->magic || to->is_big_endian != from->is_big_endian)
468*a9fa9459Szrj     {
469*a9fa9459Szrj       *err = 0;
470*a9fa9459Szrj       return "COFF object format mismatch";
471*a9fa9459Szrj     }
472*a9fa9459Szrj   return NULL;
473*a9fa9459Szrj }
474*a9fa9459Szrj 
475*a9fa9459Szrj /* Release the private data for an attributes structure.  */
476*a9fa9459Szrj 
477*a9fa9459Szrj static void
simple_object_coff_release_attributes(void * data)478*a9fa9459Szrj simple_object_coff_release_attributes (void *data)
479*a9fa9459Szrj {
480*a9fa9459Szrj   XDELETE (data);
481*a9fa9459Szrj }
482*a9fa9459Szrj 
483*a9fa9459Szrj /* Prepare to write out a file.  */
484*a9fa9459Szrj 
485*a9fa9459Szrj static void *
simple_object_coff_start_write(void * attributes_data,const char ** errmsg ATTRIBUTE_UNUSED,int * err ATTRIBUTE_UNUSED)486*a9fa9459Szrj simple_object_coff_start_write (void *attributes_data,
487*a9fa9459Szrj 				const char **errmsg ATTRIBUTE_UNUSED,
488*a9fa9459Szrj 				int *err ATTRIBUTE_UNUSED)
489*a9fa9459Szrj {
490*a9fa9459Szrj   struct simple_object_coff_attributes *attrs =
491*a9fa9459Szrj     (struct simple_object_coff_attributes *) attributes_data;
492*a9fa9459Szrj   struct simple_object_coff_attributes *ret;
493*a9fa9459Szrj 
494*a9fa9459Szrj   /* We're just going to record the attributes, but we need to make a
495*a9fa9459Szrj      copy because the user may delete them.  */
496*a9fa9459Szrj   ret = XNEW (struct simple_object_coff_attributes);
497*a9fa9459Szrj   *ret = *attrs;
498*a9fa9459Szrj   return ret;
499*a9fa9459Szrj }
500*a9fa9459Szrj 
501*a9fa9459Szrj /* Write out a COFF filehdr.  */
502*a9fa9459Szrj 
503*a9fa9459Szrj static int
simple_object_coff_write_filehdr(simple_object_write * sobj,int descriptor,unsigned int nscns,size_t symtab_offset,unsigned int nsyms,const char ** errmsg,int * err)504*a9fa9459Szrj simple_object_coff_write_filehdr (simple_object_write *sobj, int descriptor,
505*a9fa9459Szrj 				  unsigned int nscns, size_t symtab_offset,
506*a9fa9459Szrj 				  unsigned int nsyms, const char **errmsg,
507*a9fa9459Szrj 				  int *err)
508*a9fa9459Szrj {
509*a9fa9459Szrj   struct simple_object_coff_attributes *attrs =
510*a9fa9459Szrj     (struct simple_object_coff_attributes *) sobj->data;
511*a9fa9459Szrj   unsigned char hdrbuf[sizeof (struct external_filehdr)];
512*a9fa9459Szrj   unsigned char *hdr;
513*a9fa9459Szrj   void (*set_16) (unsigned char *, unsigned short);
514*a9fa9459Szrj   void (*set_32) (unsigned char *, unsigned int);
515*a9fa9459Szrj 
516*a9fa9459Szrj   hdr = &hdrbuf[0];
517*a9fa9459Szrj 
518*a9fa9459Szrj   set_16 = (attrs->is_big_endian
519*a9fa9459Szrj 	    ? simple_object_set_big_16
520*a9fa9459Szrj 	    : simple_object_set_little_16);
521*a9fa9459Szrj   set_32 = (attrs->is_big_endian
522*a9fa9459Szrj 	    ? simple_object_set_big_32
523*a9fa9459Szrj 	    : simple_object_set_little_32);
524*a9fa9459Szrj 
525*a9fa9459Szrj   memset (hdr, 0, sizeof (struct external_filehdr));
526*a9fa9459Szrj 
527*a9fa9459Szrj   set_16 (hdr + offsetof (struct external_filehdr, f_magic), attrs->magic);
528*a9fa9459Szrj   set_16 (hdr + offsetof (struct external_filehdr, f_nscns), nscns);
529*a9fa9459Szrj   /* f_timdat left as zero.  */
530*a9fa9459Szrj   set_32 (hdr + offsetof (struct external_filehdr, f_symptr), symtab_offset);
531*a9fa9459Szrj   set_32 (hdr + offsetof (struct external_filehdr, f_nsyms), nsyms);
532*a9fa9459Szrj   /* f_opthdr left as zero.  */
533*a9fa9459Szrj   set_16 (hdr + offsetof (struct external_filehdr, f_flags), attrs->flags);
534*a9fa9459Szrj 
535*a9fa9459Szrj   return simple_object_internal_write (descriptor, 0, hdrbuf,
536*a9fa9459Szrj 				       sizeof (struct external_filehdr),
537*a9fa9459Szrj 				       errmsg, err);
538*a9fa9459Szrj }
539*a9fa9459Szrj 
540*a9fa9459Szrj /* Write out a COFF section header.  */
541*a9fa9459Szrj 
542*a9fa9459Szrj static int
simple_object_coff_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)543*a9fa9459Szrj simple_object_coff_write_scnhdr (simple_object_write *sobj, int descriptor,
544*a9fa9459Szrj 				 const char *name, size_t *name_offset,
545*a9fa9459Szrj 				 off_t scnhdr_offset, size_t scnsize,
546*a9fa9459Szrj 				 off_t offset, unsigned int align,
547*a9fa9459Szrj 				 const char **errmsg, int *err)
548*a9fa9459Szrj {
549*a9fa9459Szrj   struct simple_object_coff_attributes *attrs =
550*a9fa9459Szrj     (struct simple_object_coff_attributes *) sobj->data;
551*a9fa9459Szrj   void (*set_32) (unsigned char *, unsigned int);
552*a9fa9459Szrj   unsigned char hdrbuf[sizeof (struct external_scnhdr)];
553*a9fa9459Szrj   unsigned char *hdr;
554*a9fa9459Szrj   size_t namelen;
555*a9fa9459Szrj   unsigned int flags;
556*a9fa9459Szrj 
557*a9fa9459Szrj   set_32 = (attrs->is_big_endian
558*a9fa9459Szrj 	    ? simple_object_set_big_32
559*a9fa9459Szrj 	    : simple_object_set_little_32);
560*a9fa9459Szrj 
561*a9fa9459Szrj   memset (hdrbuf, 0, sizeof hdrbuf);
562*a9fa9459Szrj   hdr = &hdrbuf[0];
563*a9fa9459Szrj 
564*a9fa9459Szrj   namelen = strlen (name);
565*a9fa9459Szrj   if (namelen <= SCNNMLEN)
566*a9fa9459Szrj     strncpy ((char *) hdr + offsetof (struct external_scnhdr, s_name), name,
567*a9fa9459Szrj 	     SCNNMLEN);
568*a9fa9459Szrj   else
569*a9fa9459Szrj     {
570*a9fa9459Szrj       snprintf ((char *) hdr + offsetof (struct external_scnhdr, s_name),
571*a9fa9459Szrj 		SCNNMLEN, "/%lu", (unsigned long) *name_offset);
572*a9fa9459Szrj       *name_offset += namelen + 1;
573*a9fa9459Szrj     }
574*a9fa9459Szrj 
575*a9fa9459Szrj   /* s_paddr left as zero.  */
576*a9fa9459Szrj   /* s_vaddr left as zero.  */
577*a9fa9459Szrj   set_32 (hdr + offsetof (struct external_scnhdr, s_size), scnsize);
578*a9fa9459Szrj   set_32 (hdr + offsetof (struct external_scnhdr, s_scnptr), offset);
579*a9fa9459Szrj   /* s_relptr left as zero.  */
580*a9fa9459Szrj   /* s_lnnoptr left as zero.  */
581*a9fa9459Szrj   /* s_nreloc left as zero.  */
582*a9fa9459Szrj   /* s_nlnno left as zero.  */
583*a9fa9459Szrj   flags = (STYP_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_SHARED
584*a9fa9459Szrj 	   | IMAGE_SCN_MEM_READ);
585*a9fa9459Szrj   /* PE can represent alignment up to 13.  */
586*a9fa9459Szrj   if (align > 13)
587*a9fa9459Szrj     align = 13;
588*a9fa9459Szrj   flags |= IMAGE_SCN_ALIGN_POWER_CONST(align);
589*a9fa9459Szrj   set_32 (hdr + offsetof (struct external_scnhdr, s_flags), flags);
590*a9fa9459Szrj 
591*a9fa9459Szrj   return simple_object_internal_write (descriptor, scnhdr_offset, hdrbuf,
592*a9fa9459Szrj 				       sizeof (struct external_scnhdr),
593*a9fa9459Szrj 				       errmsg, err);
594*a9fa9459Szrj }
595*a9fa9459Szrj 
596*a9fa9459Szrj /* Write out a complete COFF file.  */
597*a9fa9459Szrj 
598*a9fa9459Szrj static const char *
simple_object_coff_write_to_file(simple_object_write * sobj,int descriptor,int * err)599*a9fa9459Szrj simple_object_coff_write_to_file (simple_object_write *sobj, int descriptor,
600*a9fa9459Szrj 				  int *err)
601*a9fa9459Szrj {
602*a9fa9459Szrj   struct simple_object_coff_attributes *attrs =
603*a9fa9459Szrj     (struct simple_object_coff_attributes *) sobj->data;
604*a9fa9459Szrj   unsigned int nscns, secnum;
605*a9fa9459Szrj   simple_object_write_section *section;
606*a9fa9459Szrj   off_t scnhdr_offset;
607*a9fa9459Szrj   size_t symtab_offset;
608*a9fa9459Szrj   off_t secsym_offset;
609*a9fa9459Szrj   unsigned int nsyms;
610*a9fa9459Szrj   size_t offset;
611*a9fa9459Szrj   size_t name_offset;
612*a9fa9459Szrj   const char *errmsg;
613*a9fa9459Szrj   unsigned char strsizebuf[4];
614*a9fa9459Szrj   /* The interface doesn't give us access to the name of the input file
615*a9fa9459Szrj      yet.  We want to use its basename for the FILE symbol.  This is
616*a9fa9459Szrj      what 'gas' uses when told to assemble from stdin.  */
617*a9fa9459Szrj   const char *source_filename = "fake";
618*a9fa9459Szrj   size_t sflen;
619*a9fa9459Szrj   union
620*a9fa9459Szrj   {
621*a9fa9459Szrj     struct external_syment sym;
622*a9fa9459Szrj     union external_auxent aux;
623*a9fa9459Szrj   } syms[2];
624*a9fa9459Szrj   void (*set_16) (unsigned char *, unsigned short);
625*a9fa9459Szrj   void (*set_32) (unsigned char *, unsigned int);
626*a9fa9459Szrj 
627*a9fa9459Szrj   set_16 = (attrs->is_big_endian
628*a9fa9459Szrj 	    ? simple_object_set_big_16
629*a9fa9459Szrj 	    : simple_object_set_little_16);
630*a9fa9459Szrj   set_32 = (attrs->is_big_endian
631*a9fa9459Szrj 	    ? simple_object_set_big_32
632*a9fa9459Szrj 	    : simple_object_set_little_32);
633*a9fa9459Szrj 
634*a9fa9459Szrj   nscns = 0;
635*a9fa9459Szrj   for (section = sobj->sections; section != NULL; section = section->next)
636*a9fa9459Szrj     ++nscns;
637*a9fa9459Szrj 
638*a9fa9459Szrj   scnhdr_offset = sizeof (struct external_filehdr);
639*a9fa9459Szrj   offset = scnhdr_offset + nscns * sizeof (struct external_scnhdr);
640*a9fa9459Szrj   name_offset = 4;
641*a9fa9459Szrj   for (section = sobj->sections; section != NULL; section = section->next)
642*a9fa9459Szrj     {
643*a9fa9459Szrj       size_t mask;
644*a9fa9459Szrj       size_t new_offset;
645*a9fa9459Szrj       size_t scnsize;
646*a9fa9459Szrj       struct simple_object_write_section_buffer *buffer;
647*a9fa9459Szrj 
648*a9fa9459Szrj       mask = (1U << section->align) - 1;
649*a9fa9459Szrj       new_offset = offset & mask;
650*a9fa9459Szrj       new_offset &= ~ mask;
651*a9fa9459Szrj       while (new_offset > offset)
652*a9fa9459Szrj 	{
653*a9fa9459Szrj 	  unsigned char zeroes[16];
654*a9fa9459Szrj 	  size_t write;
655*a9fa9459Szrj 
656*a9fa9459Szrj 	  memset (zeroes, 0, sizeof zeroes);
657*a9fa9459Szrj 	  write = new_offset - offset;
658*a9fa9459Szrj 	  if (write > sizeof zeroes)
659*a9fa9459Szrj 	    write = sizeof zeroes;
660*a9fa9459Szrj 	  if (!simple_object_internal_write (descriptor, offset, zeroes, write,
661*a9fa9459Szrj 					     &errmsg, err))
662*a9fa9459Szrj 	    return errmsg;
663*a9fa9459Szrj 	}
664*a9fa9459Szrj 
665*a9fa9459Szrj       scnsize = 0;
666*a9fa9459Szrj       for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
667*a9fa9459Szrj 	{
668*a9fa9459Szrj 	  if (!simple_object_internal_write (descriptor, offset + scnsize,
669*a9fa9459Szrj 					     ((const unsigned char *)
670*a9fa9459Szrj 					      buffer->buffer),
671*a9fa9459Szrj 					     buffer->size, &errmsg, err))
672*a9fa9459Szrj 	    return errmsg;
673*a9fa9459Szrj 	  scnsize += buffer->size;
674*a9fa9459Szrj 	}
675*a9fa9459Szrj 
676*a9fa9459Szrj       if (!simple_object_coff_write_scnhdr (sobj, descriptor, section->name,
677*a9fa9459Szrj 					    &name_offset, scnhdr_offset,
678*a9fa9459Szrj 					    scnsize, offset, section->align,
679*a9fa9459Szrj 					    &errmsg, err))
680*a9fa9459Szrj 	return errmsg;
681*a9fa9459Szrj 
682*a9fa9459Szrj       scnhdr_offset += sizeof (struct external_scnhdr);
683*a9fa9459Szrj       offset += scnsize;
684*a9fa9459Szrj     }
685*a9fa9459Szrj 
686*a9fa9459Szrj   /* Symbol table is always half-word aligned.  */
687*a9fa9459Szrj   offset += (offset & 1);
688*a9fa9459Szrj   /* There is a file symbol and a section symbol per section,
689*a9fa9459Szrj      and each of these has a single auxiliary symbol following.  */
690*a9fa9459Szrj   nsyms = 2 * (nscns + 1);
691*a9fa9459Szrj   symtab_offset = offset;
692*a9fa9459Szrj   /* Advance across space reserved for symbol table to locate
693*a9fa9459Szrj      start of string table.  */
694*a9fa9459Szrj   offset += nsyms * sizeof (struct external_syment);
695*a9fa9459Szrj 
696*a9fa9459Szrj   /* Write out file symbol.  */
697*a9fa9459Szrj   memset (&syms[0], 0, sizeof (syms));
698*a9fa9459Szrj   strcpy ((char *)&syms[0].sym.e.e_name[0], ".file");
699*a9fa9459Szrj   set_16 (&syms[0].sym.e_scnum[0], IMAGE_SYM_DEBUG);
700*a9fa9459Szrj   set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
701*a9fa9459Szrj   syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_FILE;
702*a9fa9459Szrj   syms[0].sym.e_numaux[0] = 1;
703*a9fa9459Szrj   /* The name need not be nul-terminated if it fits into the x_fname field
704*a9fa9459Szrj      directly, but must be if it has to be placed into the string table.  */
705*a9fa9459Szrj   sflen = strlen (source_filename);
706*a9fa9459Szrj   if (sflen <= E_FILNMLEN)
707*a9fa9459Szrj     memcpy (&syms[1].aux.x_file.x_fname[0], source_filename, sflen);
708*a9fa9459Szrj   else
709*a9fa9459Szrj     {
710*a9fa9459Szrj       set_32 (&syms[1].aux.x_file.x_n.x_offset[0], name_offset);
711*a9fa9459Szrj       if (!simple_object_internal_write (descriptor, offset + name_offset,
712*a9fa9459Szrj 					 ((const unsigned char *)
713*a9fa9459Szrj 					  source_filename),
714*a9fa9459Szrj 					 sflen + 1, &errmsg, err))
715*a9fa9459Szrj 	return errmsg;
716*a9fa9459Szrj       name_offset += strlen (source_filename) + 1;
717*a9fa9459Szrj     }
718*a9fa9459Szrj   if (!simple_object_internal_write (descriptor, symtab_offset,
719*a9fa9459Szrj 				     (const unsigned char *) &syms[0],
720*a9fa9459Szrj 				     sizeof (syms), &errmsg, err))
721*a9fa9459Szrj     return errmsg;
722*a9fa9459Szrj 
723*a9fa9459Szrj   /* Write the string table length, followed by the strings and section
724*a9fa9459Szrj      symbols in step with each other.  */
725*a9fa9459Szrj   set_32 (strsizebuf, name_offset);
726*a9fa9459Szrj   if (!simple_object_internal_write (descriptor, offset, strsizebuf, 4,
727*a9fa9459Szrj 				     &errmsg, err))
728*a9fa9459Szrj     return errmsg;
729*a9fa9459Szrj 
730*a9fa9459Szrj   name_offset = 4;
731*a9fa9459Szrj   secsym_offset = symtab_offset + sizeof (syms);
732*a9fa9459Szrj   memset (&syms[0], 0, sizeof (syms));
733*a9fa9459Szrj   set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
734*a9fa9459Szrj   syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_STATIC;
735*a9fa9459Szrj   syms[0].sym.e_numaux[0] = 1;
736*a9fa9459Szrj   secnum = 1;
737*a9fa9459Szrj 
738*a9fa9459Szrj   for (section = sobj->sections; section != NULL; section = section->next)
739*a9fa9459Szrj     {
740*a9fa9459Szrj       size_t namelen;
741*a9fa9459Szrj       size_t scnsize;
742*a9fa9459Szrj       struct simple_object_write_section_buffer *buffer;
743*a9fa9459Szrj 
744*a9fa9459Szrj       namelen = strlen (section->name);
745*a9fa9459Szrj       set_16 (&syms[0].sym.e_scnum[0], secnum++);
746*a9fa9459Szrj       scnsize = 0;
747*a9fa9459Szrj       for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
748*a9fa9459Szrj 	scnsize += buffer->size;
749*a9fa9459Szrj       set_32 (&syms[1].aux.x_scn.x_scnlen[0], scnsize);
750*a9fa9459Szrj       if (namelen > SCNNMLEN)
751*a9fa9459Szrj 	{
752*a9fa9459Szrj 	  set_32 (&syms[0].sym.e.e.e_zeroes[0], 0);
753*a9fa9459Szrj 	  set_32 (&syms[0].sym.e.e.e_offset[0], name_offset);
754*a9fa9459Szrj 	  if (!simple_object_internal_write (descriptor, offset + name_offset,
755*a9fa9459Szrj 					     ((const unsigned char *)
756*a9fa9459Szrj 					      section->name),
757*a9fa9459Szrj 					     namelen + 1, &errmsg, err))
758*a9fa9459Szrj 	    return errmsg;
759*a9fa9459Szrj 	  name_offset += namelen + 1;
760*a9fa9459Szrj 	}
761*a9fa9459Szrj       else
762*a9fa9459Szrj 	{
763*a9fa9459Szrj 	  memcpy (&syms[0].sym.e.e_name[0], section->name,
764*a9fa9459Szrj 		  strlen (section->name));
765*a9fa9459Szrj 	  memset (&syms[0].sym.e.e_name[strlen (section->name)], 0,
766*a9fa9459Szrj 		  E_SYMNMLEN - strlen (section->name));
767*a9fa9459Szrj 	}
768*a9fa9459Szrj 
769*a9fa9459Szrj       if (!simple_object_internal_write (descriptor, secsym_offset,
770*a9fa9459Szrj 					 (const unsigned char *) &syms[0],
771*a9fa9459Szrj 					 sizeof (syms), &errmsg, err))
772*a9fa9459Szrj 	return errmsg;
773*a9fa9459Szrj       secsym_offset += sizeof (syms);
774*a9fa9459Szrj     }
775*a9fa9459Szrj 
776*a9fa9459Szrj   if (!simple_object_coff_write_filehdr (sobj, descriptor, nscns,
777*a9fa9459Szrj 					 symtab_offset, nsyms, &errmsg, err))
778*a9fa9459Szrj     return errmsg;
779*a9fa9459Szrj 
780*a9fa9459Szrj   return NULL;
781*a9fa9459Szrj }
782*a9fa9459Szrj 
783*a9fa9459Szrj /* Release the private data for an simple_object_write structure.  */
784*a9fa9459Szrj 
785*a9fa9459Szrj static void
simple_object_coff_release_write(void * data)786*a9fa9459Szrj simple_object_coff_release_write (void *data)
787*a9fa9459Szrj {
788*a9fa9459Szrj   XDELETE (data);
789*a9fa9459Szrj }
790*a9fa9459Szrj 
791*a9fa9459Szrj /* The COFF functions.  */
792*a9fa9459Szrj 
793*a9fa9459Szrj const struct simple_object_functions simple_object_coff_functions =
794*a9fa9459Szrj {
795*a9fa9459Szrj   simple_object_coff_match,
796*a9fa9459Szrj   simple_object_coff_find_sections,
797*a9fa9459Szrj   simple_object_coff_fetch_attributes,
798*a9fa9459Szrj   simple_object_coff_release_read,
799*a9fa9459Szrj   simple_object_coff_attributes_merge,
800*a9fa9459Szrj   simple_object_coff_release_attributes,
801*a9fa9459Szrj   simple_object_coff_start_write,
802*a9fa9459Szrj   simple_object_coff_write_to_file,
803*a9fa9459Szrj   simple_object_coff_release_write
804*a9fa9459Szrj };
805