/* simple-object-coff.c -- routines to manipulate XCOFF object files.
Copyright (C) 2013-2024 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google and David Edelsohn, IBM.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA. */
struct external_scnhdr
{
unsigned char s_name[8]; /* section name */
union
{
struct
{
unsigned char s_paddr[4]; /* physical address, aliased s_nlib */
unsigned char s_vaddr[4]; /* virtual address */
unsigned char s_size[4]; /* section size */
unsigned char s_scnptr[4]; /* file ptr to raw data for section */
unsigned char s_relptr[4]; /* file ptr to relocation */
unsigned char s_lnnoptr[4]; /* file ptr to line numbers */
unsigned char s_nreloc[2]; /* number of relocation entries */
unsigned char s_nlnno[2]; /* number of line number entries */
unsigned char s_flags[4]; /* flags */
} xcoff32;
struct
{
unsigned char s_paddr[8]; /* physical address, aliased s_nlib */
unsigned char s_vaddr[8]; /* virtual address */
unsigned char s_size[8]; /* section size */
unsigned char s_scnptr[8]; /* file ptr to raw data for section */
unsigned char s_relptr[8]; /* file ptr to relocation */
unsigned char s_lnnoptr[8]; /* file ptr to line numbers */
unsigned char s_nreloc[4]; /* number of relocation entries */
unsigned char s_nlnno[4]; /* number of line number entries */
unsigned char s_flags[4]; /* flags */
} xcoff64;
} u;
};
#define SCNHSZ32 (40)
#define SCNHSZ64 (68)
/* The length of the s_name field in struct external_scnhdr. */
#define SCNNMLEN (8)
/* Bits for scnhdr s_flags field. */
#define STYP_DATA 0x40
/* XCOFF symbol table entry. */
#define N_SYMNMLEN (8) /* # characters in a symbol name */
/* The format of an XCOFF symbol-table entry. */
struct external_syment
{
union {
struct {
union {
/* The name of the symbol. There is an implicit null character
after the end of the array. */
char n_name[N_SYMNMLEN];
struct {
/* If n_zeroes is zero, n_offset is the offset the name from
the start of the string table. */
unsigned char n_zeroes[4];
unsigned char n_offset[4];
} n;
} n;
/* The symbol's value. */
unsigned char n_value[4];
} xcoff32;
struct {
/* The symbol's value. */
unsigned char n_value[8];
/* The offset of the symbol from the start of the string table. */
unsigned char n_offset[4];
} xcoff64;
} u;
/* The number of the section to which this symbol belongs. */
unsigned char n_scnum[2];
/* The type of symbol. (It can be interpreted as an n_lang
and an n_cpu byte, but we don't care about that here.) */
unsigned char n_type[2];
/* The class of symbol (a C_* value). */
unsigned char n_sclass[1];
/* The number of auxiliary symbols attached to this entry. */
unsigned char n_numaux[1];
};
#define SYMESZ (18)
/* Length allowed for filename in aux sym format 4. */
#define FILNMLEN (14)
/* Omits x_sym and other unused variants. */
union external_auxent
{
/* Aux sym format 4: file. */
union
{
char x_fname[FILNMLEN];
struct
{
unsigned char x_zeroes[4];
unsigned char x_offset[4];
unsigned char x_pad[FILNMLEN-8];
unsigned char x_ftype;
} _x;
} x_file;
/* Aux sym format 5: section. */
struct
{
unsigned char x_scnlen[4]; /* section length */
unsigned char x_nreloc[2]; /* # relocation entries */
unsigned char x_nlinno[2]; /* # line numbers */
} x_scn;
/* CSECT auxiliary entry. */
union
{
struct
{
struct
{
unsigned char x_scnlen[4]; /* csect length */
unsigned char x_parmhash[4]; /* parm type hash index */
unsigned char x_snhash[2]; /* sect num with parm hash */
unsigned char x_smtyp; /* symbol align and type */
unsigned char x_smclas; /* storage mapping class */
unsigned char x_stab; /* dbx stab info index */
unsigned char x_snstab[2]; /* sect num with dbx stab */
} x_csect;
} xcoff32;
struct
{
struct
{
unsigned char x_scnlen_lo[4]; /* csect length */
unsigned char x_parmhash[4]; /* parm type hash index */
unsigned char x_snhash[2]; /* sect num with parm hash */
unsigned char x_smtyp; /* symbol align and type */
unsigned char x_smclas; /* storage mapping class */
unsigned char x_scnlen_hi[4];
unsigned char pad;
unsigned char x_auxtype;
} x_csect;
} xcoff64;
} u;
/* SECTION/DWARF auxiliary entry. */
struct
{
unsigned char x_scnlen[4]; /* section length */
unsigned char pad1[4];
unsigned char x_nreloc[4]; /* number RLDs */
} x_sect;
};
struct simple_object_xcoff_read
{
/* Magic number. */
unsigned short magic;
/* Number of sections. */
unsigned short nscns;
/* File offset of symbol table. */
off_t symptr;
/* Number of symbol table entries. */
unsigned int nsyms;
/* Flags. */
unsigned short flags;
/* Offset of section headers in file. */
off_t scnhdr_offset;
};
/* Private data for an simple_object_attributes. */
struct simple_object_xcoff_attributes
{
/* Magic number. */
unsigned short magic;
/* Flags. */
unsigned short flags;
};
/* See if we have a XCOFF file. */
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)
{
unsigned short magic;
unsigned short (*fetch_16) (const unsigned char *);
unsigned int (*fetch_32) (const unsigned char *);
ulong_type (*fetch_64) (const unsigned char *);
unsigned char hdrbuf[sizeof (struct external_filehdr)];
struct simple_object_xcoff_read *ocr;
int u64;
strindex = strtol (namebuf + 1, &end, 10);
if (*end == '\0')
{
/* The real section name is found in the string
table. */
if (strtab == NULL)
{
strtab = simple_object_xcoff_read_strtab (sobj,
&strtab_size,
&errmsg, err);
if (strtab == NULL)
{
XDELETEVEC (scnbuf);
return errmsg;
}
}
if (strindex < 4 || strindex >= strtab_size)
{
XDELETEVEC (strtab);
XDELETEVEC (scnbuf);
*err = 0;
return "section string index out of range";
}
/* Search in symbol table if we have a ".go_export" symbol. */
for (i = 0; i < ocr->nsyms; i += n_numaux + 1)
{
sym = (unsigned char *) &symtab[i];
n_numaux = symtab[i].n_numaux[0];
if (symtab[i].n_sclass[0] != C_EXT
&& symtab[i].n_sclass[0] != C_HIDEXT)
continue;
/* Must have at least one csect auxiliary entry. */
if (n_numaux < 1 || i + n_numaux >= ocr->nsyms)
continue;
/* The real symbol name is found in the string table. */
if (strtab == NULL)
{
strtab = simple_object_xcoff_read_strtab (sobj,
&strtab_size,
&errmsg, err);
if (strtab == NULL)
{
XDELETEVEC (symtab);
XDELETEVEC (scnbuf);
return errmsg;
}
}
if (n_offset >= strtab_size)
{
XDELETEVEC (strtab);
XDELETEVEC (symtab);
XDELETEVEC (scnbuf);
*err = 0;
return "symbol string index out of range";
}
n_name = strtab + n_offset;
if (!strcmp (n_name, ".go_export"))
{
union external_auxent *auxent;
unsigned char *aux, *scnhdr;
off_t scnptr, x_scnlen;
/* Found .go_export symbol, read its csect auxiliary entry.
By convention, it is the last auxiliary entry. */
auxent = (union external_auxent *) &symtab[i + n_numaux];
aux = (unsigned char *) auxent;
if (u64)
{
/* Use an intermediate 64-bit type to avoid
compilation warning about 32-bit shift below on
hosts with 32-bit off_t which aren't supported by
AC_SYS_LARGEFILE. */
ulong_type x_scnlen64;
/* We're just going to record the attributes, but we need to make a
copy because the user may delete them. */
ret = XNEW (struct simple_object_xcoff_attributes);
*ret = *attrs;
return ret;
}
static const char *
simple_object_xcoff_write_to_file (simple_object_write *sobj, int descriptor,
int *err)
{
struct simple_object_xcoff_read *ocr =
(struct simple_object_xcoff_read *) sobj->data;
int u64 = ocr->magic == U64_TOCMAGIC;
unsigned int nscns, secnum;
simple_object_write_section *section;
off_t scnhdr_offset;
size_t symtab_offset;
off_t secsym_offset;
unsigned int nsyms;
size_t offset;
size_t name_offset;
const char *errmsg;
unsigned char strsizebuf[4];
/* The interface doesn't give us access to the name of the input file
yet. We want to use its basename for the FILE symbol. This is
what 'gas' uses when told to assemble from stdin. */
const char *source_filename = "fake";
size_t sflen;
union
{
struct external_syment sym;
union external_auxent aux;
} syms[2];
void (*set_16) (unsigned char *, unsigned short);
void (*set_32) (unsigned char *, unsigned int);
/* Symbol table is always half-word aligned. */
offset += (offset & 1);
/* There is a file symbol and a section symbol per section,
and each of these has a single auxiliary symbol following. */
nsyms = 2 * (nscns + 1);
symtab_offset = offset;
/* Advance across space reserved for symbol table to locate
start of string table. */
offset += nsyms * SYMESZ;
/* Write out file symbol. */
memset (&syms[0], 0, sizeof (syms));
if (!u64)
strcpy ((char *)&syms[0].sym.u.xcoff32.n.n_name[0], ".file");
set_16 (&syms[0].sym.n_scnum[0], N_DEBUG);
set_16 (&syms[0].sym.n_type[0], IMAGE_SYM_TYPE);
syms[0].sym.n_sclass[0] = C_FILE;
syms[0].sym.n_numaux[0] = 1;
/* The name need not be nul-terminated if it fits into the x_fname field
directly, but must be if it has to be placed into the string table. */
sflen = strlen (source_filename);
if (sflen <= FILNMLEN)
memcpy (&syms[1].aux.x_file.x_fname[0], source_filename, sflen);
else
{
set_32 (&syms[1].aux.x_file._x.x_offset[0], name_offset);
if (!simple_object_internal_write (descriptor, offset + name_offset,
((const unsigned char *)
source_filename),
sflen + 1, &errmsg, err))
return errmsg;
name_offset += strlen (source_filename) + 1;
}
if (!simple_object_internal_write (descriptor, symtab_offset,
(const unsigned char *) &syms[0],
sizeof (syms), &errmsg, err))
return errmsg;
/* Write the string table length, followed by the strings and section
symbols in step with each other. */
set_32 (strsizebuf, name_offset);
if (!simple_object_internal_write (descriptor, offset, strsizebuf, 4,
&errmsg, err))
return errmsg;