1 /* simple-object-coff.c -- routines to manipulate COFF object files.
2 Copyright (C) 2010-2018 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
21 #include "libiberty.h"
22 #include "simple-object.h"
39 #ifdef HAVE_INTTYPES_H
43 #include "simple-object-common.h"
45 /* COFF structures and constants. */
47 /* COFF file header. */
49 struct external_filehdr
51 unsigned char f_magic[2]; /* magic number */
52 unsigned char f_nscns[2]; /* number of sections */
53 unsigned char f_timdat[4]; /* time & date stamp */
54 unsigned char f_symptr[4]; /* file pointer to symtab */
55 unsigned char f_nsyms[4]; /* number of symtab entries */
56 unsigned char f_opthdr[2]; /* sizeof(optional hdr) */
57 unsigned char f_flags[2]; /* flags */
60 /* Bits for filehdr f_flags field. */
62 #define F_EXEC (0x0002)
63 #define IMAGE_FILE_SYSTEM (0x1000)
64 #define IMAGE_FILE_DLL (0x2000)
66 /* COFF section header. */
68 struct external_scnhdr
70 unsigned char s_name[8]; /* section name */
71 unsigned char s_paddr[4]; /* physical address, aliased s_nlib */
72 unsigned char s_vaddr[4]; /* virtual address */
73 unsigned char s_size[4]; /* section size */
74 unsigned char s_scnptr[4]; /* file ptr to raw data for section */
75 unsigned char s_relptr[4]; /* file ptr to relocation */
76 unsigned char s_lnnoptr[4]; /* file ptr to line numbers */
77 unsigned char s_nreloc[2]; /* number of relocation entries */
78 unsigned char s_nlnno[2]; /* number of line number entries */
79 unsigned char s_flags[4]; /* flags */
82 /* The length of the s_name field in struct external_scnhdr. */
86 /* Bits for scnhdr s_flags field. This includes some bits defined
87 only for PE. This may need to be moved into coff_magic. */
89 #define STYP_DATA (1 << 6)
90 #define IMAGE_SCN_MEM_DISCARDABLE (1 << 25)
91 #define IMAGE_SCN_MEM_SHARED (1 << 28)
92 #define IMAGE_SCN_MEM_READ (1 << 30)
94 #define IMAGE_SCN_ALIGN_POWER_BIT_POS 20
95 #define IMAGE_SCN_ALIGN_POWER_CONST(val) \
96 (((val) + 1) << IMAGE_SCN_ALIGN_POWER_BIT_POS)
98 /* COFF symbol table entry. */
100 #define E_SYMNMLEN 8 /* # characters in a symbol name */
102 struct external_syment
106 unsigned char e_name[E_SYMNMLEN];
110 unsigned char e_zeroes[4];
111 unsigned char e_offset[4];
115 unsigned char e_value[4];
116 unsigned char e_scnum[2];
117 unsigned char e_type[2];
118 unsigned char e_sclass[1];
119 unsigned char e_numaux[1];
122 /* Length allowed for filename in aux sym format 4. */
124 #define E_FILNMLEN 18
126 /* Omits x_sym and other unused variants. */
128 union external_auxent
130 /* Aux sym format 4: file. */
133 char x_fname[E_FILNMLEN];
136 unsigned char x_zeroes[4];
137 unsigned char x_offset[4];
140 /* Aux sym format 5: section. */
143 unsigned char x_scnlen[4]; /* section length */
144 unsigned char x_nreloc[2]; /* # relocation entries */
145 unsigned char x_nlinno[2]; /* # line numbers */
146 unsigned char x_checksum[4]; /* section COMDAT checksum */
147 unsigned char x_associated[2]; /* COMDAT assoc section index */
148 unsigned char x_comdat[1]; /* COMDAT selection number */
152 /* Symbol-related constants. */
154 #define IMAGE_SYM_DEBUG (-2)
155 #define IMAGE_SYM_TYPE_NULL (0)
156 #define IMAGE_SYM_DTYPE_NULL (0)
157 #define IMAGE_SYM_CLASS_STATIC (3)
158 #define IMAGE_SYM_CLASS_FILE (103)
160 #define IMAGE_SYM_TYPE \
161 ((IMAGE_SYM_DTYPE_NULL << 4) | IMAGE_SYM_TYPE_NULL)
163 /* Private data for an simple_object_read. */
165 struct simple_object_coff_read
168 unsigned short magic;
169 /* Whether the file is big-endian. */
170 unsigned char is_big_endian;
171 /* Number of sections. */
172 unsigned short nscns;
173 /* File offset of symbol table. */
175 /* Number of symbol table entries. */
178 unsigned short flags;
179 /* Offset of section headers in file. */
183 /* Private data for an simple_object_attributes. */
185 struct simple_object_coff_attributes
188 unsigned short magic;
189 /* Whether the file is big-endian. */
190 unsigned char is_big_endian;
192 unsigned short flags;
195 /* There is no magic number which indicates a COFF file as opposed to
196 any other sort of file. Instead, each COFF file starts with a
197 two-byte magic number which also indicates the type of the target.
198 This struct holds a magic number as well as characteristics of that
201 struct coff_magic_struct
204 unsigned short magic;
205 /* Whether this magic number is for a big-endian file. */
206 unsigned char is_big_endian;
207 /* Flag bits, in the f_flags fields, which indicates that this file
208 is not a relocatable object file. There is no flag which
209 specifically indicates a relocatable object file, it is only
210 implied by the absence of these flags. */
211 unsigned short non_object_flags;
214 /* This is a list of the COFF magic numbers which we recognize, namely
215 the ones used on Windows. More can be added as needed. */
217 static const struct coff_magic_struct coff_magic[] =
220 { 0x14c, 0, F_EXEC | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL },
222 { 0x8664, 0, F_EXEC | IMAGE_FILE_SYSTEM | IMAGE_FILE_DLL }
225 /* See if we have a COFF file. */
228 simple_object_coff_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
229 int descriptor, off_t offset,
230 const char *segment_name ATTRIBUTE_UNUSED,
231 const char **errmsg, int *err)
234 unsigned short magic_big;
235 unsigned short magic_little;
236 unsigned short magic;
239 unsigned short (*fetch_16) (const unsigned char *);
240 unsigned int (*fetch_32) (const unsigned char *);
241 unsigned char hdrbuf[sizeof (struct external_filehdr)];
242 unsigned short flags;
243 struct simple_object_coff_read *ocr;
245 c = sizeof (coff_magic) / sizeof (coff_magic[0]);
246 magic_big = simple_object_fetch_big_16 (header);
247 magic_little = simple_object_fetch_little_16 (header);
248 for (i = 0; i < c; ++i)
250 if (coff_magic[i].is_big_endian
251 ? coff_magic[i].magic == magic_big
252 : coff_magic[i].magic == magic_little)
261 is_big_endian = coff_magic[i].is_big_endian;
263 magic = is_big_endian ? magic_big : magic_little;
264 fetch_16 = (is_big_endian
265 ? simple_object_fetch_big_16
266 : simple_object_fetch_little_16);
267 fetch_32 = (is_big_endian
268 ? simple_object_fetch_big_32
269 : simple_object_fetch_little_32);
271 if (!simple_object_internal_read (descriptor, offset, hdrbuf, sizeof hdrbuf,
275 flags = fetch_16 (hdrbuf + offsetof (struct external_filehdr, f_flags));
276 if ((flags & coff_magic[i].non_object_flags) != 0)
278 *errmsg = "not relocatable object file";
283 ocr = XNEW (struct simple_object_coff_read);
285 ocr->is_big_endian = is_big_endian;
286 ocr->nscns = fetch_16 (hdrbuf + offsetof (struct external_filehdr, f_nscns));
287 ocr->symptr = fetch_32 (hdrbuf
288 + offsetof (struct external_filehdr, f_symptr));
289 ocr->nsyms = fetch_32 (hdrbuf + offsetof (struct external_filehdr, f_nsyms));
291 ocr->scnhdr_offset = (sizeof (struct external_filehdr)
292 + fetch_16 (hdrbuf + offsetof (struct external_filehdr,
298 /* Read the string table in a COFF file. */
301 simple_object_coff_read_strtab (simple_object_read *sobj, size_t *strtab_size,
302 const char **errmsg, int *err)
304 struct simple_object_coff_read *ocr =
305 (struct simple_object_coff_read *) sobj->data;
307 unsigned char strsizebuf[4];
311 strtab_offset = sobj->offset + ocr->symptr
312 + ocr->nsyms * sizeof (struct external_syment);
313 if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
314 strsizebuf, 4, errmsg, err))
316 strsize = (ocr->is_big_endian
317 ? simple_object_fetch_big_32 (strsizebuf)
318 : simple_object_fetch_little_32 (strsizebuf));
319 strtab = XNEWVEC (char, strsize);
320 if (!simple_object_internal_read (sobj->descriptor, strtab_offset,
321 (unsigned char *) strtab, strsize, errmsg,
327 *strtab_size = strsize;
331 /* Find all sections in a COFF file. */
334 simple_object_coff_find_sections (simple_object_read *sobj,
335 int (*pfn) (void *, const char *,
336 off_t offset, off_t length),
340 struct simple_object_coff_read *ocr =
341 (struct simple_object_coff_read *) sobj->data;
343 unsigned char *scnbuf;
345 unsigned int (*fetch_32) (const unsigned char *);
351 scnhdr_size = sizeof (struct external_scnhdr);
352 scnbuf = XNEWVEC (unsigned char, scnhdr_size * ocr->nscns);
353 if (!simple_object_internal_read (sobj->descriptor,
354 sobj->offset + ocr->scnhdr_offset,
355 scnbuf, scnhdr_size * ocr->nscns, &errmsg,
362 fetch_32 = (ocr->is_big_endian
363 ? simple_object_fetch_big_32
364 : simple_object_fetch_little_32);
369 for (i = 0; i < nscns; ++i)
371 unsigned char *scnhdr;
372 unsigned char *scnname;
373 char namebuf[SCNNMLEN + 1];
378 scnhdr = scnbuf + i * scnhdr_size;
379 scnname = scnhdr + offsetof (struct external_scnhdr, s_name);
380 memcpy (namebuf, scnname, SCNNMLEN);
381 namebuf[SCNNMLEN] = '\0';
383 if (namebuf[0] == '/')
388 strindex = strtol (namebuf + 1, &end, 10);
391 /* The real section name is found in the string
395 strtab = simple_object_coff_read_strtab (sobj,
405 if (strindex < 4 || strindex >= strtab_size)
410 return "section string index out of range";
413 name = strtab + strindex;
417 scnptr = fetch_32 (scnhdr + offsetof (struct external_scnhdr, s_scnptr));
418 size = fetch_32 (scnhdr + offsetof (struct external_scnhdr, s_size));
420 if (!(*pfn) (data, name, scnptr, size))
431 /* Fetch the attributes for an simple_object_read. */
434 simple_object_coff_fetch_attributes (simple_object_read *sobj,
435 const char **errmsg ATTRIBUTE_UNUSED,
436 int *err ATTRIBUTE_UNUSED)
438 struct simple_object_coff_read *ocr =
439 (struct simple_object_coff_read *) sobj->data;
440 struct simple_object_coff_attributes *ret;
442 ret = XNEW (struct simple_object_coff_attributes);
443 ret->magic = ocr->magic;
444 ret->is_big_endian = ocr->is_big_endian;
445 ret->flags = ocr->flags;
449 /* Release the private data for an simple_object_read. */
452 simple_object_coff_release_read (void *data)
457 /* Compare two attributes structures. */
460 simple_object_coff_attributes_merge (void *todata, void *fromdata, int *err)
462 struct simple_object_coff_attributes *to =
463 (struct simple_object_coff_attributes *) todata;
464 struct simple_object_coff_attributes *from =
465 (struct simple_object_coff_attributes *) fromdata;
467 if (to->magic != from->magic || to->is_big_endian != from->is_big_endian)
470 return "COFF object format mismatch";
475 /* Release the private data for an attributes structure. */
478 simple_object_coff_release_attributes (void *data)
483 /* Prepare to write out a file. */
486 simple_object_coff_start_write (void *attributes_data,
487 const char **errmsg ATTRIBUTE_UNUSED,
488 int *err ATTRIBUTE_UNUSED)
490 struct simple_object_coff_attributes *attrs =
491 (struct simple_object_coff_attributes *) attributes_data;
492 struct simple_object_coff_attributes *ret;
494 /* We're just going to record the attributes, but we need to make a
495 copy because the user may delete them. */
496 ret = XNEW (struct simple_object_coff_attributes);
501 /* Write out a COFF filehdr. */
504 simple_object_coff_write_filehdr (simple_object_write *sobj, int descriptor,
505 unsigned int nscns, size_t symtab_offset,
506 unsigned int nsyms, const char **errmsg,
509 struct simple_object_coff_attributes *attrs =
510 (struct simple_object_coff_attributes *) sobj->data;
511 unsigned char hdrbuf[sizeof (struct external_filehdr)];
513 void (*set_16) (unsigned char *, unsigned short);
514 void (*set_32) (unsigned char *, unsigned int);
518 set_16 = (attrs->is_big_endian
519 ? simple_object_set_big_16
520 : simple_object_set_little_16);
521 set_32 = (attrs->is_big_endian
522 ? simple_object_set_big_32
523 : simple_object_set_little_32);
525 memset (hdr, 0, sizeof (struct external_filehdr));
527 set_16 (hdr + offsetof (struct external_filehdr, f_magic), attrs->magic);
528 set_16 (hdr + offsetof (struct external_filehdr, f_nscns), nscns);
529 /* f_timdat left as zero. */
530 set_32 (hdr + offsetof (struct external_filehdr, f_symptr), symtab_offset);
531 set_32 (hdr + offsetof (struct external_filehdr, f_nsyms), nsyms);
532 /* f_opthdr left as zero. */
533 set_16 (hdr + offsetof (struct external_filehdr, f_flags), attrs->flags);
535 return simple_object_internal_write (descriptor, 0, hdrbuf,
536 sizeof (struct external_filehdr),
540 /* Write out a COFF section header. */
543 simple_object_coff_write_scnhdr (simple_object_write *sobj, int descriptor,
544 const char *name, size_t *name_offset,
545 off_t scnhdr_offset, size_t scnsize,
546 off_t offset, unsigned int align,
547 const char **errmsg, int *err)
549 struct simple_object_coff_attributes *attrs =
550 (struct simple_object_coff_attributes *) sobj->data;
551 void (*set_32) (unsigned char *, unsigned int);
552 unsigned char hdrbuf[sizeof (struct external_scnhdr)];
557 set_32 = (attrs->is_big_endian
558 ? simple_object_set_big_32
559 : simple_object_set_little_32);
561 memset (hdrbuf, 0, sizeof hdrbuf);
564 namelen = strlen (name);
565 if (namelen <= SCNNMLEN)
566 strncpy ((char *) hdr + offsetof (struct external_scnhdr, s_name), name,
570 snprintf ((char *) hdr + offsetof (struct external_scnhdr, s_name),
571 SCNNMLEN, "/%lu", (unsigned long) *name_offset);
572 *name_offset += namelen + 1;
575 /* s_paddr left as zero. */
576 /* s_vaddr left as zero. */
577 set_32 (hdr + offsetof (struct external_scnhdr, s_size), scnsize);
578 set_32 (hdr + offsetof (struct external_scnhdr, s_scnptr), offset);
579 /* s_relptr left as zero. */
580 /* s_lnnoptr left as zero. */
581 /* s_nreloc left as zero. */
582 /* s_nlnno left as zero. */
583 flags = (STYP_DATA | IMAGE_SCN_MEM_DISCARDABLE | IMAGE_SCN_MEM_SHARED
584 | IMAGE_SCN_MEM_READ);
585 /* PE can represent alignment up to 13. */
588 flags |= IMAGE_SCN_ALIGN_POWER_CONST(align);
589 set_32 (hdr + offsetof (struct external_scnhdr, s_flags), flags);
591 return simple_object_internal_write (descriptor, scnhdr_offset, hdrbuf,
592 sizeof (struct external_scnhdr),
596 /* Write out a complete COFF file. */
599 simple_object_coff_write_to_file (simple_object_write *sobj, int descriptor,
602 struct simple_object_coff_attributes *attrs =
603 (struct simple_object_coff_attributes *) sobj->data;
604 unsigned int nscns, secnum;
605 simple_object_write_section *section;
607 size_t symtab_offset;
613 unsigned char strsizebuf[4];
614 /* The interface doesn't give us access to the name of the input file
615 yet. We want to use its basename for the FILE symbol. This is
616 what 'gas' uses when told to assemble from stdin. */
617 const char *source_filename = "fake";
621 struct external_syment sym;
622 union external_auxent aux;
624 void (*set_16) (unsigned char *, unsigned short);
625 void (*set_32) (unsigned char *, unsigned int);
627 set_16 = (attrs->is_big_endian
628 ? simple_object_set_big_16
629 : simple_object_set_little_16);
630 set_32 = (attrs->is_big_endian
631 ? simple_object_set_big_32
632 : simple_object_set_little_32);
635 for (section = sobj->sections; section != NULL; section = section->next)
638 scnhdr_offset = sizeof (struct external_filehdr);
639 offset = scnhdr_offset + nscns * sizeof (struct external_scnhdr);
641 for (section = sobj->sections; section != NULL; section = section->next)
646 struct simple_object_write_section_buffer *buffer;
648 mask = (1U << section->align) - 1;
649 new_offset = offset & mask;
650 new_offset &= ~ mask;
651 while (new_offset > offset)
653 unsigned char zeroes[16];
656 memset (zeroes, 0, sizeof zeroes);
657 write = new_offset - offset;
658 if (write > sizeof zeroes)
659 write = sizeof zeroes;
660 if (!simple_object_internal_write (descriptor, offset, zeroes, write,
666 for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
668 if (!simple_object_internal_write (descriptor, offset + scnsize,
669 ((const unsigned char *)
671 buffer->size, &errmsg, err))
673 scnsize += buffer->size;
676 if (!simple_object_coff_write_scnhdr (sobj, descriptor, section->name,
677 &name_offset, scnhdr_offset,
678 scnsize, offset, section->align,
682 scnhdr_offset += sizeof (struct external_scnhdr);
686 /* Symbol table is always half-word aligned. */
687 offset += (offset & 1);
688 /* There is a file symbol and a section symbol per section,
689 and each of these has a single auxiliary symbol following. */
690 nsyms = 2 * (nscns + 1);
691 symtab_offset = offset;
692 /* Advance across space reserved for symbol table to locate
693 start of string table. */
694 offset += nsyms * sizeof (struct external_syment);
696 /* Write out file symbol. */
697 memset (&syms[0], 0, sizeof (syms));
698 strcpy ((char *)&syms[0].sym.e.e_name[0], ".file");
699 set_16 (&syms[0].sym.e_scnum[0], IMAGE_SYM_DEBUG);
700 set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
701 syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_FILE;
702 syms[0].sym.e_numaux[0] = 1;
703 /* The name need not be nul-terminated if it fits into the x_fname field
704 directly, but must be if it has to be placed into the string table. */
705 sflen = strlen (source_filename);
706 if (sflen <= E_FILNMLEN)
707 memcpy (&syms[1].aux.x_file.x_fname[0], source_filename, sflen);
710 set_32 (&syms[1].aux.x_file.x_n.x_offset[0], name_offset);
711 if (!simple_object_internal_write (descriptor, offset + name_offset,
712 ((const unsigned char *)
714 sflen + 1, &errmsg, err))
716 name_offset += strlen (source_filename) + 1;
718 if (!simple_object_internal_write (descriptor, symtab_offset,
719 (const unsigned char *) &syms[0],
720 sizeof (syms), &errmsg, err))
723 /* Write the string table length, followed by the strings and section
724 symbols in step with each other. */
725 set_32 (strsizebuf, name_offset);
726 if (!simple_object_internal_write (descriptor, offset, strsizebuf, 4,
731 secsym_offset = symtab_offset + sizeof (syms);
732 memset (&syms[0], 0, sizeof (syms));
733 set_16 (&syms[0].sym.e_type[0], IMAGE_SYM_TYPE);
734 syms[0].sym.e_sclass[0] = IMAGE_SYM_CLASS_STATIC;
735 syms[0].sym.e_numaux[0] = 1;
738 for (section = sobj->sections; section != NULL; section = section->next)
742 struct simple_object_write_section_buffer *buffer;
744 namelen = strlen (section->name);
745 set_16 (&syms[0].sym.e_scnum[0], secnum++);
747 for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
748 scnsize += buffer->size;
749 set_32 (&syms[1].aux.x_scn.x_scnlen[0], scnsize);
750 if (namelen > SCNNMLEN)
752 set_32 (&syms[0].sym.e.e.e_zeroes[0], 0);
753 set_32 (&syms[0].sym.e.e.e_offset[0], name_offset);
754 if (!simple_object_internal_write (descriptor, offset + name_offset,
755 ((const unsigned char *)
757 namelen + 1, &errmsg, err))
759 name_offset += namelen + 1;
763 memcpy (&syms[0].sym.e.e_name[0], section->name,
764 strlen (section->name));
765 memset (&syms[0].sym.e.e_name[strlen (section->name)], 0,
766 E_SYMNMLEN - strlen (section->name));
769 if (!simple_object_internal_write (descriptor, secsym_offset,
770 (const unsigned char *) &syms[0],
771 sizeof (syms), &errmsg, err))
773 secsym_offset += sizeof (syms);
776 if (!simple_object_coff_write_filehdr (sobj, descriptor, nscns,
777 symtab_offset, nsyms, &errmsg, err))
783 /* Release the private data for an simple_object_write structure. */
786 simple_object_coff_release_write (void *data)
791 /* The COFF functions. */
793 const struct simple_object_functions simple_object_coff_functions =
795 simple_object_coff_match,
796 simple_object_coff_find_sections,
797 simple_object_coff_fetch_attributes,
798 simple_object_coff_release_read,
799 simple_object_coff_attributes_merge,
800 simple_object_coff_release_attributes,
801 simple_object_coff_start_write,
802 simple_object_coff_write_to_file,
803 simple_object_coff_release_write,