1 /* COFF information for TI COFF support. Definitions in this file should be
2 customized in a target-specific file, and then this file included (see
3 tic54x.h for an example).
5 Copyright 2001 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Note "coff/external.h is not used because TI adds extra fields to the structures. */
25 /********************** FILE HEADER **********************/
27 struct external_filehdr
29 char f_magic
[2]; /* magic number */
30 char f_nscns
[2]; /* number of sections */
31 char f_timdat
[4]; /* time & date stamp */
32 char f_symptr
[4]; /* file pointer to symtab */
33 char f_nsyms
[4]; /* number of symtab entries */
34 char f_opthdr
[2]; /* sizeof(optional hdr) */
35 char f_flags
[2]; /* flags */
36 char f_target_id
[2]; /* magic no. (TI COFF-specific) */
39 /* COFF0 has magic number in f_magic, and omits f_target_id from the file
40 header; for later versions, f_magic is 0xC1 for COFF1 and 0xC2 for COFF2
41 and the target-specific magic number is found in f_target_id */
43 #define TICOFF0MAGIC TI_TARGET_ID
44 #define TICOFF1MAGIC 0x00C1
45 #define TICOFF2MAGIC 0x00C2
46 #define TICOFF_AOUT_MAGIC 0x0108 /* magic number in optional header */
47 #define TICOFF 1 /* customize coffcode.h */
49 /* The target_id field changes depending on the particular CPU target */
50 /* for COFF0, the target id appeared in f_magic, where COFFX magic is now */
52 #error "TI_TARGET_ID needs to be defined for your CPU"
55 /* Which bfd_arch to use... */
56 #ifndef TICOFF_TARGET_ARCH
57 #error "TICOFF_TARGET_ARCH needs to be defined for your CPU"
60 /* Default to COFF2 for file output */
61 #ifndef TICOFF_DEFAULT_MAGIC
62 #define TICOFF_DEFAULT_MAGIC TICOFF2MAGIC
65 /* This value is made available in the rare case where a bfd is unavailable */
66 #ifndef OCTETS_PER_BYTE_POWER
67 #error "OCTETS_PER_BYTE_POWER not defined for this CPU"
69 #define OCTETS_PER_BYTE (1<<OCTETS_PER_BYTE_POWER)
72 /* default alignment is on a byte (not octet!) boundary */
73 #ifndef COFF_DEFAULT_SECTION_ALIGNMENT_POWER
74 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 0
77 /* TI COFF encodes the section alignment in the section header flags */
78 #define COFF_ALIGN_IN_SECTION_HEADER 1
79 #define COFF_ALIGN_IN_S_FLAGS 1
80 /* requires a power-of-two argument */
81 #define COFF_ENCODE_ALIGNMENT(S,X) ((S).s_flags |= (((unsigned)(X)&0xF)<<8))
82 /* result is a power of two */
83 #define COFF_DECODE_ALIGNMENT(X) (((X)>>8)&0xF)
85 #define COFF0_P(ABFD) (bfd_coff_filhsz(ABFD) == FILHSZ_V0)
86 #define COFF2_P(ABFD) (bfd_coff_scnhsz(ABFD) != SCNHSZ_V01)
88 #define COFF0_BADMAG(x) ((x).f_magic != TICOFF0MAGIC)
89 #define COFF1_BADMAG(x) ((x).f_magic != TICOFF1MAGIC || (x).f_target_id != TI_TARGET_ID)
90 #define COFF2_BADMAG(x) ((x).f_magic != TICOFF2MAGIC || (x).f_target_id != TI_TARGET_ID)
92 /* we need to read/write an extra field in the coff file header */
93 #ifndef COFF_ADJUST_FILEHDR_IN_POST
94 #define COFF_ADJUST_FILEHDR_IN_POST(abfd,src,dst) \
95 do { ((struct internal_filehdr *)(dst))->f_target_id = \
96 bfd_h_get_16(abfd, (bfd_byte *)(((FILHDR *)(src))->f_target_id)); \
100 #ifndef COFF_ADJUST_FILEHDR_OUT_POST
101 #define COFF_ADJUST_FILEHDR_OUT_POST(abfd,src,dst) \
102 do { bfd_h_put_16(abfd, ((struct internal_filehdr *)(src))->f_target_id, \
103 (bfd_byte *)(((FILHDR *)(dst))->f_target_id)); \
107 #define FILHDR struct external_filehdr
109 #define FILHSZ_V0 20 /* COFF0 omits target_id field */
111 /* File header flags */
112 #define F_RELFLG (0x0001)
113 #define F_EXEC (0x0002)
114 #define F_LNNO (0x0004)
115 /* F_LSYMS needs to be redefined in your source file */
116 #define F_LSYMS_TICOFF (0x0010) /* normal COFF is 0x8 */
118 #define F_10 0x00 /* file built for TMS320C1x devices */
119 #define F_20 0x10 /* file built for TMS320C2x devices */
120 #define F_25 0x20 /* file built for TMS320C2x/C5x devices */
121 #define F_LENDIAN 0x0100 /* 16 bits/word, LSB first */
122 #define F_SYMMERGE 0x1000 /* duplicate symbols were removed */
124 /********************** OPTIONAL HEADER **********************/
129 char magic
[2]; /* type of file (0x108) */
130 char vstamp
[2]; /* version stamp */
131 char tsize
[4]; /* text size in bytes, padded to FW bdry*/
132 char dsize
[4]; /* initialized data " " */
133 char bsize
[4]; /* uninitialized data " " */
134 char entry
[4]; /* entry pt. */
135 char text_start
[4]; /* base of text used for this file */
136 char data_start
[4]; /* base of data used for this file */
145 /********************** SECTION HEADER **********************/
147 struct external_scnhdr_v01
{
148 char s_name
[8]; /* section name */
149 char s_paddr
[4]; /* physical address, aliased s_nlib */
150 char s_vaddr
[4]; /* virtual address */
151 char s_size
[4]; /* section size (in WORDS) */
152 char s_scnptr
[4]; /* file ptr to raw data for section */
153 char s_relptr
[4]; /* file ptr to relocation */
154 char s_lnnoptr
[4]; /* file ptr to line numbers */
155 char s_nreloc
[2]; /* number of relocation entries */
156 char s_nlnno
[2]; /* number of line number entries*/
157 char s_flags
[2]; /* flags */
158 char s_reserved
[1]; /* reserved */
159 char s_page
[1]; /* section page number (LOAD) */
163 struct external_scnhdr
{
164 char s_name
[8]; /* section name */
165 char s_paddr
[4]; /* physical address, aliased s_nlib */
166 char s_vaddr
[4]; /* virtual address */
167 char s_size
[4]; /* section size (in WORDS) */
168 char s_scnptr
[4]; /* file ptr to raw data for section */
169 char s_relptr
[4]; /* file ptr to relocation */
170 char s_lnnoptr
[4]; /* file ptr to line numbers */
171 char s_nreloc
[4]; /* number of relocation entries */
172 char s_nlnno
[4]; /* number of line number entries*/
173 char s_flags
[4]; /* flags */
174 char s_reserved
[2]; /* reserved */
175 char s_page
[2]; /* section page number (LOAD) */
179 * Special section flags
182 /* TI COFF defines these flags;
183 STYP_CLINK: the section should be excluded from the final
184 linker output if there are no references found to any symbol in the section
185 STYP_BLOCK: the section should be blocked, i.e. if the section would cross
186 a page boundary, it is started at a page boundary instead.
187 TI COFF puts the section alignment power of two in the section flags
188 e.g. 2**N is alignment, flags |= (N & 0xF) << 8
190 #define STYP_CLINK (0x4000)
191 #define STYP_BLOCK (0x1000)
192 #define STYP_ALIGN (0x0F00) /* TI COFF stores section alignment here */
194 #define SCNHDR_V01 struct external_scnhdr_v01
195 #define SCNHDR struct external_scnhdr
196 #define SCNHSZ_V01 40 /* for v0 and v1 */
199 /* COFF2 changes the offsets and sizes of these fields
200 Assume we're dealing with the COFF2 scnhdr structure, and adjust
203 #define GET_SCNHDR_NRELOC(ABFD,PTR) \
204 (COFF2_P(ABFD) ? bfd_h_get_32 (ABFD,PTR) : bfd_h_get_16 (ABFD, PTR))
205 #define PUT_SCNHDR_NRELOC(ABFD,VAL,PTR) \
206 (COFF2_P(ABFD) ? bfd_h_put_32 (ABFD,VAL,PTR) : bfd_h_put_16 (ABFD,VAL,PTR))
207 #define GET_SCNHDR_NLNNO(ABFD,PTR) \
208 (COFF2_P(ABFD) ? bfd_h_get_32 (ABFD,PTR) : bfd_h_get_16 (ABFD, (PTR)-2))
209 #define PUT_SCNHDR_NLNNO(ABFD,VAL,PTR) \
210 (COFF2_P(ABFD) ? bfd_h_put_32 (ABFD,VAL,PTR) : bfd_h_put_16 (ABFD,VAL,(PTR)-2))
211 #define GET_SCNHDR_FLAGS(ABFD,PTR) \
212 (COFF2_P(ABFD) ? bfd_h_get_32 (ABFD,PTR) : bfd_h_get_16 (ABFD, (PTR)-4))
213 #define PUT_SCNHDR_FLAGS(ABFD,VAL,PTR) \
214 (COFF2_P(ABFD) ? bfd_h_put_32 (ABFD,VAL,PTR) : bfd_h_put_16 (ABFD,VAL,(PTR)-4))
215 #define GET_SCNHDR_PAGE(ABFD,PTR) \
216 (COFF2_P(ABFD) ? bfd_h_get_16 (ABFD,PTR) : bfd_h_get_8 (ABFD, (PTR)-7))
217 /* on output, make sure that the "reserved" field is zero */
218 #define PUT_SCNHDR_PAGE(ABFD,VAL,PTR) \
219 (COFF2_P(ABFD) ? bfd_h_put_16 (ABFD,VAL,PTR) : \
220 bfd_h_put_8 (ABFD,VAL,(PTR)-7), bfd_h_put_8 (ABFD, 0, (PTR)-8))
222 /* TI COFF stores section size as number of bytes (address units, not octets),
223 so adjust to be number of octets, which is what BFD expects */
224 #define GET_SCNHDR_SIZE(ABFD,SZP) \
225 (bfd_h_get_32(ABFD,SZP)*bfd_octets_per_byte(ABFD))
226 #define PUT_SCNHDR_SIZE(ABFD,SZ,SZP) \
227 bfd_h_put_32(ABFD,(SZ)/bfd_octets_per_byte(ABFD),SZP)
229 #define COFF_ADJUST_SCNHDR_IN_POST(ABFD,EXT,INT) \
230 do { ((struct internal_scnhdr *)(INT))->s_page = \
231 GET_SCNHDR_PAGE(ABFD,(bfd_byte *)((SCNHDR *)(EXT))->s_page); \
234 /* The line number and reloc overflow checking in coff_swap_scnhdr_out in
235 coffswap.h doesn't use PUT_X for s_nlnno and s_nreloc.
236 Due to different sized v0/v1/v2 section headers, we have to re-write these
239 #define COFF_ADJUST_SCNHDR_OUT_POST(ABFD,INT,EXT) \
241 PUT_SCNHDR_NLNNO(ABFD,((struct internal_scnhdr *)(INT))->s_nlnno,\
242 (bfd_byte *)((SCNHDR *)(EXT))->s_nlnno); \
243 PUT_SCNHDR_NRELOC(ABFD,((struct internal_scnhdr *)(INT))->s_nreloc,\
244 (bfd_byte *)((SCNHDR *)(EXT))->s_nreloc); \
245 PUT_SCNHDR_FLAGS(ABFD,((struct internal_scnhdr *)(INT))->s_flags, \
246 (bfd_byte *)((SCNHDR *)(EXT))->s_flags); \
247 PUT_SCNHDR_PAGE(ABFD,((struct internal_scnhdr *)(INT))->s_page, \
248 (bfd_byte *)((SCNHDR *)(EXT))->s_page); \
253 The first GDB port requires flags in its remote memory access commands to
254 distinguish between data/prog space. Hopefully we can make this go away
255 eventually. Stuff the page in the upper bits of a 32-bit address, since
256 the c5x family only uses 16 or 23 bits.
258 c2x, c5x and most c54x devices have 16-bit addresses, but the c548 has
259 23-bit program addresses. Make sure the page flags don't interfere.
260 These flags are used by GDB to identify the destination page for
264 /* recognized load pages */
265 #define PG_PROG 0x0 /* PROG page */
266 #define PG_DATA 0x1 /* DATA page */
268 #define ADDR_MASK 0x00FFFFFF
269 #define PG_TO_FLAG(p) (((unsigned long)(p) & 0xFF) << 24)
270 #define FLAG_TO_PG(f) (((f) >> 24) & 0xFF)
273 * names of "special" sections
275 #define _TEXT ".text"
276 #define _DATA ".data"
278 #define _CINIT ".cinit" /* initialized C data */
279 #define _SCONST ".const" /* constants */
280 #define _SWITCH ".switch" /* switch tables */
281 #define _STACK ".stack" /* C stack */
282 #define _SYSMEM ".sysmem" /* used for malloc et al. syscalls */
284 /********************** LINE NUMBERS **********************/
286 /* 1 line number entry for every "breakpointable" source line in a section.
287 * Line numbers are grouped on a per function basis; first entry in a function
288 * grouping will have l_lnno = 0 and in place of physical address will be the
289 * symbol table index of the function name.
291 struct external_lineno
{
293 char l_symndx
[4]; /* function name symbol index, iff l_lnno == 0*/
294 char l_paddr
[4]; /* (physical) address of line number */
296 char l_lnno
[2]; /* line number */
299 #define LINENO struct external_lineno
303 /********************** SYMBOLS **********************/
305 /* NOTE: this is what a local label looks like in assembly source; what it
306 looks like in COFF output is undefined */
307 #define TICOFF_LOCAL_LABEL_P(NAME) \
308 ((NAME[0] == '$' && NAME[1] >= '0' && NAME[1] <= '9' && NAME[2] == '\0') \
309 || NAME[strlen(NAME)-1] == '?')
311 #define E_SYMNMLEN 8 /* # characters in a symbol name */
312 #define E_FILNMLEN 14 /* # characters in a file name */
313 #define E_DIMNUM 4 /* # array dimensions in auxiliary entry */
315 struct external_syment
318 char e_name
[E_SYMNMLEN
];
332 #define N_BTMASK (017)
333 #define N_TMASK (060)
338 union external_auxent
{
340 char x_tagndx
[4]; /* str, un, or enum tag indx */
343 char x_lnno
[2]; /* declaration line number */
344 char x_size
[2]; /* str/union/array size */
346 char x_fsize
[4]; /* size of function */
349 struct { /* if ISFCN, tag, or .bb */
350 char x_lnnoptr
[4]; /* ptr to fcn line # */
351 char x_endndx
[4]; /* entry ndx past block end */
353 struct { /* if ISARY, up to 4 dimen. */
354 char x_dimen
[E_DIMNUM
][2];
357 char x_tvndx
[2]; /* tv index */
361 char x_fname
[E_FILNMLEN
];
369 char x_scnlen
[4]; /* section length */
370 char x_nreloc
[2]; /* # relocation entries */
371 char x_nlinno
[2]; /* # line numbers */
375 char x_tvfill
[4]; /* tv fill value */
376 char x_tvlen
[2]; /* length of .tv */
377 char x_tvran
[2][2]; /* tv range */
378 } x_tv
; /* info about .tv section (in auxent of symbol .tv)) */
383 #define SYMENT struct external_syment
385 #define AUXENT union external_auxent
388 /* section lengths are in target bytes (not host bytes) */
389 #define GET_SCN_SCNLEN(ABFD,EXT) \
390 (bfd_h_get_32(ABFD,(bfd_byte *)(EXT)->x_scn.x_scnlen)*bfd_octets_per_byte(ABFD))
391 #define PUT_SCN_SCNLEN(ABFD,INT,EXT) \
392 bfd_h_put_32(ABFD,(INT)/bfd_octets_per_byte(ABFD),\
393 (bfd_byte *)(EXT)->x_scn.x_scnlen)
395 /* lnsz size is in bits in COFF file, in bytes in BFD */
396 #define GET_LNSZ_SIZE(abfd, ext) \
397 (bfd_h_get_16(abfd, (bfd_byte *)ext->x_sym.x_misc.x_lnsz.x_size) / \
398 (class != C_FIELD ? 8 : 1))
400 #define PUT_LNSZ_SIZE(abfd, in, ext) \
401 bfd_h_put_16(abfd, ((class != C_FIELD) ? (in)*8 : (in)), \
402 (bfd_byte*) ext->x_sym.x_misc.x_lnsz.x_size)
404 /* TI COFF stores offsets for MOS and MOU in bits; BFD expects bytes */
405 #define COFF_ADJUST_SYM_IN_POST(ABFD,EXT,INT) \
406 do { struct internal_syment *dst = (struct internal_syment *)(INT); \
407 if (dst->n_sclass == C_MOS || dst->n_sclass == C_MOU) dst->n_value /= 8; \
410 #define COFF_ADJUST_SYM_OUT_POST(ABFD,INT,EXT) \
411 do { struct internal_syment *src = (struct internal_syment *)(INT); \
412 SYMENT *dst = (SYMENT *)(EXT); \
413 if(src->n_sclass == C_MOU || src->n_sclass == C_MOS) \
414 bfd_h_put_32(abfd,src->n_value * 8,(bfd_byte *)dst->e_value); \
417 /* Detect section-relative absolute symbols so they get flagged with a sym
420 #define SECTION_RELATIVE_ABSOLUTE_SYMBOL_P(RELOC,SECT) \
421 ((*(RELOC)->sym_ptr_ptr)->section->output_section == (SECT) \
422 && (RELOC)->howto->name[0] == 'A')
424 /********************** RELOCATION DIRECTIVES **********************/
426 struct external_reloc_v0
434 struct external_reloc
438 char r_reserved
[2]; /* extended pmad byte for COFF2 */
442 #define RELOC struct external_reloc
443 #define RELSZ_V0 10 /* FIXME -- coffcode.h needs fixing */
444 #define RELSZ 12 /* for COFF1/2 */
446 /* various relocation types. */
447 #define R_ABS 0x0000 /* no relocation */
448 #define R_REL13 0x002A /* 13-bit direct reference (???) */
449 #define R_PARTLS7 0x0028 /* 7 LSBs of an address */
450 #define R_PARTMS9 0x0029 /* 9MSBs of an address */
451 #define R_EXTWORD 0x002B /* 23-bit direct reference */
452 #define R_EXTWORD16 0x002C /* 16-bit direct reference to 23-bit addr*/
453 #define R_EXTWORDMS7 0x002D /* upper 7 bits of 23-bit address */
455 #endif /* COFF_TI_H */