COFF(5) — FILE FORMATS
NAME
coff − common assembler and link editor output
SYNOPSIS
#include <filehdr.h>
#include <aouthdr.h>
#include <scnhdr.h>
#include <reloc.h>
#include <linenum.h>
#include <storclass.h>
#include <syms.h>
AVAILABILITY
Sun386i systems only.
DESCRIPTION
The output from the link editor and the assembler (named a.out by default) is in COFF format (Common Object File Format) on the Sun386i system.
A common object file consists of a file header, a system header (if the file is link editor output), a table of section headers, a data section, relocation information, (optional) line numbers, a symbol table, and a string table. The general format looks like this:
file-header
system-header
section-headers
data
relocation
line-numbers
symbol-table
string-table
section-headers contains a number of section headers:
section 1 header
...
section n header
Similarly, data, relocation, and line-numbers are each divided into n sections.
The last three parts of an object file (line numbers, symbol table and string table) may be missing if the program was linked with the −s option of ld(1) or if they were removed by strip(1). Also note that the relocation information will be absent after linking unless the −r option of ld(1) was used. The string table exists only if the symbol table contains symbols with names longer than eight characters.
The sizes of each section (contained in the header, discussed below) are in bytes.
When an a.out file is loaded into memory for execution, three logical segments are set up: the text segment, the data segment (initialized data followed by uninitialized, the latter actually being initialized to all 0’s), and a stack. The text segment starts at location 0x1000 by default.
The a.out file produced by ld(1) has the magic number 0413 in the first field of the system header. The headers (file header, system header, and section headers) are loaded at the beginning of the text segment and the text immediately follows the headers in the user address space. The first text address will equal 0x1000 plus the size of the headers, and will vary depending upon the number of section headers in the a.out file. In an a.out file with three sections (.text, .data, .bss, and .comment), the first text address is at 0x000010D0. The text segment is not writable by the program; if other processes are executing the same a.out file, the processes will share a single text segment.
The data segment starts at the next 4K boundary past the last text address. The first data address is determined by the following: If an a.out file were split into 4K chunks, one of the chunks would contain both the end of text and the beginning of data. When the a.out file is loaded into memory for execution, that chunk will appear twice; once at the end of text and once at the beginning of data (with some unused space in between). The duplicated chunk of text that appears at the beginning of data is never executed; it is duplicated so that the operating system may bring in pieces of the file in multiples of the page size without having to realign the beginning of the data section to a page boundary. Therefore the first data address is the sum of the next segment boundary past the end of text plus the remainder of the last text address divided by 4K. If the last text address is a multiple of 4K no duplication is necessary.
On the Sun386i computer the stack begins at location 0xFBFFFFFF and grows toward lower addresses. The stack is automatically extended as required. The data segment is extended only as requested by the brk(2) system call.
For relocatable files the value of a word in the text or data portions that is not a reference to an undefined external symbol is exactly the value that will appear in memory when the file is executed. If a word in the text involves a reference to an undefined external symbol, there will be a relocation entry for the word, the storage class of the symbol-table entry for the symbol will be marked as an “external symbol”, and the value and section number of the symbol-table entry will be undefined. When the file is processed by the link editor and the external symbol becomes defined, the value of the symbol will be added to the word in the file.
File Header
The format of the file header is:
struct filehdr
{
unsigned shortf_magic; /∗ magic number ∗/
unsigned shortf_nscns; /∗ number of sections ∗/
longf_timdat;/∗ time and date stamp ∗/
longf_symptr;/∗ file ptr to symtab ∗/
longf_nsyms;/∗ # symtab entries ∗/
unsigned shortf_opthdr; /∗ sizeof(opt hdr) ∗/
unsigned shortf_flags; /∗ flags ∗/
};
System Header
The format of the system header is:
typedef struct aouthdr
{
shortmagic;/∗ magic number ∗/
shortvstamp;/∗ version stamp ∗/
longtsize;/∗ text size in bytes, padded ∗/
longdsize;/∗ initialized data (.data) ∗/
longbsize;/∗ uninitialized data (.bss) ∗/
longentry;/∗ entry point ∗/
longtext_start;/∗ base of text used for this file ∗/
longdata_start;/∗ base of data used for this file ∗/
} AOUTHDR;
Section Header
The format of the section header is:
struct scnhdr
{
chars_name[SYMNMLEN];/∗ section name ∗/
longs_paddr;/∗ physical address ∗/
longs_vaddr;/∗ virtual address ∗/
longs_size;/∗ section size ∗/
longs_scnptr;/∗ file ptr to raw data ∗/
longs_relptr;/∗ file ptr to relocation ∗/
longs_lnnoptr; /∗ file ptr to line numbers ∗/
unsigned shorts_nreloc; /∗ # reloc entries ∗/
unsigned shorts_nlnno; /∗ # line number entries ∗/
longs_flags;/∗ flags ∗/
};
Relocation
Object files have one relocation entry for each relocatable reference in the text or data. If relocation information is present, it will be in the following format:
struct reloc
{
longr_vaddr;/∗ (virtual) address of reference ∗/
longr_symndx; /∗ index into symbol table ∗/
ushortr_type;/∗ relocation type ∗/
};
#defineR_IND1603
The start of the relocation information is s_relptr from the section header. If there is no relocation information, s_relptr is 0.
Line Number
The cc(1V) command generates an entry in the object file for each C source line on which a breakpoint is possible (when invoked with the −g option. Users can refer to line numbers when using the appropriate debugger, such as dbx(1)). The structure of these line number entries appears below.
structlineno
{
union
{
longl_symndx ;
longl_paddr ;
}l_addr ;
unsigned short l_lnno ;
} ;
Numbering starts with one at the top of the source file and increments independent of transition between functions. The initial line number entry for a function has l_lnno equal to zero, and the symbol table index of the function’s entry is in l_symndx. Otherwise, l_lnno is non-zero, and l_paddr is the physical address of the code for the referenced line. Thus the overall structure is the following:
l_addrl_lnno
function symtab index0
physical addressline
physical addressline
...
function symtab index0
physical addressline
physical addressline
...
Symbol Table
The format of each symbol in the symbol table is described by the syment structure, shown below. This structure is compatible with System V COFF, but has an added _n_dbx structure which is needed by dbx (1).
#define SYMNMLEN8
#define FILNMLEN14
#define DIMNUM4
struct syment
{
union/∗ all ways to get a symbol name ∗/
{
char_n_name[SYMNMLEN]; /∗ name of symbol ∗/
struct
{
long_n_zeroes;/∗ == 0L if in string table ∗/
long_n_offset;/∗ location in string table ∗/
} _n_n;
char∗_n_nptr[2];/∗ allows overlaying ∗/
struct
{
char_n_leading_zero;/∗ null char ∗/
char_n_dbx_type;/∗ stab type ∗/
short_n_dbx_desc;/∗ value of desc field ∗/
long_n_stab_ptr;/∗ table ptr ∗/
} _n_dbx;
} _n;
longn_value;/∗ value of symbol ∗/
shortn_scnum;/∗ section number ∗/
unsigned shortn_type;/∗ type and derived type ∗/
charn_sclass;/∗ storage class ∗/
charn_numaux;/∗ number of aux entries ∗/
};
#define n_name_n._n_name
#define n_zeroes_n._n_n._n_zeroes
#define n_offset_n._n_n._n_offset
#define n_nptr_n._n_nptr[1]
The storage class member (n_sclass) is set to one of the constants defined in <storclass.h>. Some symbols require more information than a single entry; they are followed by auxiliary entries that are the same size as a symbol entry. The format follows.
union auxent {
struct {
longx_tagndx;
union {
struct {
unsigned short x_lnno;
unsigned short x_size;
} x_lnsz;
longx_fsize;
} x_misc;
union {
struct {
longx_lnnoptr;
longx_endndx;
} x_fcn;
struct {
unsigned short x_dimen[DIMNUM];
} x_ary;
} x_fcnary;
unsigned short x_tvndx;
} x_sym;
struct {
charx_fname[FILNMLEN];
} x_file;
struct {
long x_scnlen;
unsigned short x_nreloc;
unsigned short x_nlinno;
} x_scn;
struct {
longx_tvfill;
unsigned shortx_tvlen;
unsigned shortx_tvran[2];
} x_tv;
};
Indexes of symbol table entries begin at zero. The start of the symbol table is f_symptr (from the file header) bytes from the beginning of the file. If the symbol table is stripped, f_symptr is 0. The string table (if one exists) begins at f_symptr + (f_nsyms ∗ SYMESZ) bytes from the beginning of the file.
SEE ALSO
as(1), cc(1V), ld(1), brk(2), ldfcn(3)
Sun Release 4.0 — Last change: 22 March 1989