G Common Object File Format (COFF)
Overall structure 630
File header 632
Optional header 633
Section headers 634
Raw data sections 636
COFF relocation information 637
Line number information 639
Symbol table 641
Additional symbols 643
String table 643
This section describes the Common Object File Format, COFF, used by the linker.
For further information on COFF, including the meaning of debugging symbols generated by Wind River compilers, see Understanding and Using COFF, Gircys, Gintaras R., O'Reilly & Associates, Inc., November, 1988.
The COFF Object Format is used both for object files (.o extension) and executable files. Some of the information is only present in object files, other information is only present in the executable files.
Table G-1 COFF file components
The following figure shows the COFF file structure:
The file header contains general information about the object file and has the following structure from the file filehdr.h:
struct filehdr {
unsigned short f_magic; /* magic */
unsigned short f_nscns; /* number of sections */
long f_timdat; /* date stamp */
long f_symptr; /* fileptr to symtab */
long f_nsyms; /* symtab count */
unsigned short f_opthdr; /* sizeof(optional hdr) */
unsigned short f_flags; /* flags */
};
The optional header contains information about an executable file and has the following structure from the file aouthdr.h:
typedef struct aouthdr {
short magic; /* a.out magic */
short vstamp; /* version stamp */
long tsize; /* .text size */
long dsize; /* .data size */
long bsize; /* .bss size */
long entry; /* entry point */
long text_start; /* fileptr to .text */
long data_start; /* fileptr to .data */
} AOUTHDR;
Table G-3 COFF optional (executable) header fields
There is one section header for each section in the COFF file, specified by the f_nscns field in the COFF File Header. Section headers have the following structure from the file scnhdr.h:
struct scnhdr { /* modified COFF*/
char s_name[8]; /* section name */
long s_paddr; /* physical address */
long s_vaddr; /* virtual address */
long s_size; /* size of section */
long s_scnptr; /* fileptr to raw data*/
long s_relptr; /* fileptr to reloc */
long s_lnnoptr; /* fileptr to lineno */
unsigned long short s_nreloc; /* reloc count */
unsigned long short s_nlnno; /* line number count */
long s_flags; /* flags */
};
#define SCNHDR struct scnhdr
#define SCNHSZ sizeof(SCNHDR)
Table G-4 COFF section header fields
The following table shows the correspondence between the type-spec as defined on p.409 and the COFF section flags assigned to the output section.
Table G-5 type-spec - COFF section flag correspondence
| type-spec | Section flags (s_flags) |
|---|---|
The Raw Data Sections contain the actual raw data for each section.
Small constant data; see the Set size limit for "small const" variables (-Xsmall-const=n), p.106. | |
Small initialized data; see the Set size limit for "small data" variables (-Xsmall-data=n), p.106. | |
Code that is to be executed when the user program has finished execution. | |
The instructions of the .fini code; the .init, .fini, and .eini sections should be placed after each other in memory. |
The Relocation Information segment contains information about unresolved references. Since compilers and assemblers do not know at what absolute memory address a symbol will be allocated, and since they are unaware of definitions of symbols in other files, every reference to such a symbol will create a relocation entry. The relocation entry will point to the address where the reference is being made, and to the symbol table entry that contains the symbol that is referenced. The linker will use this information to fill in the correct address after it has allocated addresses to all symbols.
When an offset is added to a symbol in the assembly source,
lwz r3,(var+16)(r0)
move.l var+16,d0
that offset is stored in the addressing mode, so that adding the real address of the symbol with the address field will yield a correct reference.
The relocation segment does not exist in executable files.
A relocation entry has the following structure from the file reloc.h:
struct reloc { /* modified COFF */
long r_vaddr; /* address of reference */
long r_symndx; /* index into symtab */
unsigned short r_type; /* relocation type */
unsigned short r_offset; /* hi word of rel addr */
};
#define RELOC struct reloc
#define RELSZ sizeof(RELOC)
#define RELSZ 10 /* sizeof(RELOC) */
Table G-7 COFF relocation entry fields
Table G-8 COFF relocation types
The line number information segment contains the mapping from source line numbers to machine instruction addresses used by symbolic debuggers. This information is only available if the -g option is specified to the compiler.
Line number entries for a section form groups of pairs where the first pair in a group is a pointer to the function containing the source. After that, every source line that has generated any instruction has an entry specifying the line number relative to the beginning of the function, and the corresponding instruction address. Normally only the .text section has line number information. The following table demonstrates the layout of the line number entries:
A line number entry has the following structure from the file linenum.h:
struct lineno {
union {
long l_symndx;
long l_paddr;
} l_addr;
unsigned long short l_lnno;
};
#define LINENO struct lineno
#define LINESZ sizeof(LINENO)
#define LINESZ 6
Table G-9 COFF line number fields
| Field | Description |
|---|---|
Symbol table index for a new function; only valid if l_lnno is set to zero. | |
Instruction address corresponding to the source line l_lnno. | |
The symbol table is an array of entries containing information about the symbols referenced in the COFF file. A symbol table entry has the following structure from the file syms.h:
struct syment {
union {
char _n_name[8];
struct {
long _n_zeroes;
long _n_offset;
} _n_n;
char *_n_nptr[2]
} _n;
long n_value;
short n_scnum;
unsigned short n_type;
char n_sclass;
char n_numaux;
short n_pad;
};
#define SYMENT struct syment
#define SYMESZ c 20
#define SYMESZ 18
#define n_name _n._n_name
#define n_nptr _n._n_nptr[1]
#define n_zeroes _n._n_n._n_zeroes
#define n_offset _n._n_n._n_offset
Table G-10 COFF symbol table fields
Any auxiliary entries to a symbol are stored immediately after the symbol in the table. They are mainly used for symbolic debugging (-g option) and are not discussed here.
Wind River uses special COFF symbols as follows:
Table G-11 Special COFF Symbols
| Extension | Description |
|---|---|
COMDAT-section-name. See Mark sections as COMDAT for linker collapse (-Xcomdat), p.71. | |
Section flags (a: allocate, w: write, x: execute, b: bss/nocode). | |
Weak symbol. See weak pragma, p.138. |
The string table contains the null terminated names of symbols longer than eight characters. Those symbols point into the string table through an offset, n_offset. The first four bytes of the string table contain the size of the table and after that all strings are stored sequentially.
support@windriver.com
Copyright © 2002, Wind River Systems, Inc. All rights reserved.