Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / binutils / include / coff / ti.h
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).
4    
5    Copyright 2001 Free Software Foundation, Inc.
6
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.
11    
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.
16    
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.  */
20 #ifndef COFF_TI_H
21 #define COFF_TI_H
22
23 /* Note "coff/external.h is not used because TI adds extra fields to the structures.  */
24
25 /********************** FILE HEADER **********************/
26
27 struct external_filehdr
28   {
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) */
37   };
38
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 */ 
42
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 */
48
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 */
51 #ifndef TI_TARGET_ID
52 #error "TI_TARGET_ID needs to be defined for your CPU"
53 #endif
54
55 /* Which bfd_arch to use... */
56 #ifndef TICOFF_TARGET_ARCH
57 #error "TICOFF_TARGET_ARCH needs to be defined for your CPU"
58 #endif
59
60 /* Default to COFF2 for file output */
61 #ifndef TICOFF_DEFAULT_MAGIC
62 #define TICOFF_DEFAULT_MAGIC TICOFF2MAGIC
63 #endif
64
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"
68 #else
69 #define OCTETS_PER_BYTE (1<<OCTETS_PER_BYTE_POWER)
70 #endif
71
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
75 #endif
76
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)
84
85 #define COFF0_P(ABFD) (bfd_coff_filhsz(ABFD) == FILHSZ_V0)
86 #define COFF2_P(ABFD) (bfd_coff_scnhsz(ABFD) != SCNHSZ_V01)
87
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)
91
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                                                                    \
96     {                                                                   \
97       ((struct internal_filehdr *)(dst))->f_target_id =                 \
98         H_GET_16 (abfd, ((FILHDR *)(src))->f_target_id);                \
99     }                                                                   \
100   while (0)
101 #endif
102
103 #ifndef COFF_ADJUST_FILEHDR_OUT_POST
104 #define COFF_ADJUST_FILEHDR_OUT_POST(abfd, src, dst) \
105   do                                                                    \
106     {                                                                   \
107       H_PUT_16 (abfd, ((struct internal_filehdr *)(src))->f_target_id,  \
108                ((FILHDR *)(dst))->f_target_id);                         \
109     }                                                                   \
110   while (0)
111 #endif
112
113 #define FILHDR  struct external_filehdr
114 #define FILHSZ  22
115 #define FILHSZ_V0 20                /* COFF0 omits target_id field */
116
117 /* File header flags */
118 #define F_RELFLG        (0x0001)
119 #define F_EXEC          (0x0002)
120 #define F_LNNO          (0x0004)
121 /* F_LSYMS needs to be redefined in your source file */
122 #define F_LSYMS_TICOFF  (0x0010) /* normal COFF is 0x8 */
123
124 #define F_10            0x00    /* file built for TMS320C1x devices */
125 #define F_20            0x10    /* file built for TMS320C2x devices */
126 #define F_25            0x20    /* file built for TMS320C2x/C5x devices */
127 #define F_LENDIAN       0x0100  /* 16 bits/word, LSB first */
128 #define F_SYMMERGE      0x1000  /* duplicate symbols were removed */
129
130 /********************** OPTIONAL HEADER **********************/
131
132
133 typedef struct 
134 {
135   char  magic[2];               /* type of file (0x108)                 */
136   char  vstamp[2];              /* version stamp                        */
137   char  tsize[4];               /* text size in bytes, padded to FW bdry*/
138   char  dsize[4];               /* initialized data "  "                */
139   char  bsize[4];               /* uninitialized data "   "             */
140   char  entry[4];               /* entry pt.                            */
141   char  text_start[4];          /* base of text used for this file */
142   char  data_start[4];          /* base of data used for this file */
143 }
144 AOUTHDR;
145
146
147 #define AOUTHDRSZ 28
148 #define AOUTSZ 28
149
150
151 /********************** SECTION HEADER **********************/
152 /* COFF0, COFF1 */
153 struct external_scnhdr_v01 {
154         char            s_name[8];      /* section name                 */
155         char            s_paddr[4];     /* physical address, aliased s_nlib */
156         char            s_vaddr[4];     /* virtual address              */
157         char            s_size[4];      /* section size (in WORDS)      */
158         char            s_scnptr[4];    /* file ptr to raw data for section */
159         char            s_relptr[4];    /* file ptr to relocation       */
160         char            s_lnnoptr[4];   /* file ptr to line numbers     */
161         char            s_nreloc[2];    /* number of relocation entries */
162         char            s_nlnno[2];     /* number of line number entries*/
163         char            s_flags[2];     /* flags                        */
164         char            s_reserved[1];  /* reserved                     */ 
165         char            s_page[1];      /* section page number (LOAD)   */
166 };
167
168 /* COFF2 */
169 struct external_scnhdr {
170         char            s_name[8];      /* section name                 */
171         char            s_paddr[4];     /* physical address, aliased s_nlib */
172         char            s_vaddr[4];     /* virtual address              */
173         char            s_size[4];      /* section size (in WORDS)      */
174         char            s_scnptr[4];    /* file ptr to raw data for section */
175         char            s_relptr[4];    /* file ptr to relocation       */
176         char            s_lnnoptr[4];   /* file ptr to line numbers     */
177         char            s_nreloc[4];    /* number of relocation entries */
178         char            s_nlnno[4];     /* number of line number entries*/
179         char            s_flags[4];     /* flags                        */
180         char            s_reserved[2];  /* reserved                     */ 
181         char            s_page[2];      /* section page number (LOAD)   */
182 };
183
184 /*
185  * Special section flags
186  */
187
188 /* TI COFF defines these flags; 
189    STYP_CLINK: the section should be excluded from the final
190    linker output if there are no references found to any symbol in the section
191    STYP_BLOCK: the section should be blocked, i.e. if the section would cross
192    a page boundary, it is started at a page boundary instead.
193    TI COFF puts the section alignment power of two in the section flags
194    e.g. 2**N is alignment, flags |= (N & 0xF) << 8
195 */ 
196 #define STYP_CLINK      (0x4000)
197 #define STYP_BLOCK      (0x1000)
198 #define STYP_ALIGN      (0x0F00) /* TI COFF stores section alignment here */
199
200 #define SCNHDR_V01 struct external_scnhdr_v01
201 #define SCNHDR struct external_scnhdr
202 #define SCNHSZ_V01 40                  /* for v0 and v1 */
203 #define SCNHSZ 48
204
205 /* COFF2 changes the offsets and sizes of these fields 
206    Assume we're dealing with the COFF2 scnhdr structure, and adjust
207    accordingly 
208  */
209 #define GET_SCNHDR_NRELOC(ABFD, PTR) \
210   (COFF2_P (ABFD) ? H_GET_32 (ABFD, PTR) : H_GET_16 (ABFD, PTR))
211 #define PUT_SCNHDR_NRELOC(ABFD, VAL, PTR) \
212   (COFF2_P (ABFD) ? H_PUT_32 (ABFD, VAL, PTR) : H_PUT_16 (ABFD, VAL, PTR))
213 #define GET_SCNHDR_NLNNO(ABFD, PTR) \
214   (COFF2_P (ABFD) ? H_GET_32 (ABFD, PTR) : H_GET_16 (ABFD, (PTR) -2))
215 #define PUT_SCNHDR_NLNNO(ABFD, VAL, PTR) \
216   (COFF2_P (ABFD) ? H_PUT_32 (ABFD, VAL, PTR) : H_PUT_16 (ABFD, VAL, (PTR) -2))
217 #define GET_SCNHDR_FLAGS(ABFD, PTR) \
218   (COFF2_P (ABFD) ? H_GET_32 (ABFD, PTR) : H_GET_16 (ABFD, (PTR) -4))
219 #define PUT_SCNHDR_FLAGS(ABFD, VAL, PTR) \
220   (COFF2_P (ABFD) ? H_PUT_32 (ABFD, VAL, PTR) : H_PUT_16 (ABFD, VAL, (PTR) -4))
221 #define GET_SCNHDR_PAGE(ABFD, PTR) \
222   (COFF2_P (ABFD) ? H_GET_16 (ABFD, PTR) : (unsigned) H_GET_8 (ABFD, (PTR) -7))
223 /* on output, make sure that the "reserved" field is zero */
224 #define PUT_SCNHDR_PAGE(ABFD, VAL, PTR) \
225   (COFF2_P (ABFD) \
226    ? H_PUT_16 (ABFD, VAL, PTR) \
227    : H_PUT_8 (ABFD, VAL, (PTR) -7), H_PUT_8 (ABFD, 0, (PTR) -8))
228
229 /* TI COFF stores section size as number of bytes (address units, not octets),
230    so adjust to be number of octets, which is what BFD expects */ 
231 #define GET_SCNHDR_SIZE(ABFD, SZP) \
232   (H_GET_32 (ABFD, SZP) * bfd_octets_per_byte (ABFD))
233 #define PUT_SCNHDR_SIZE(ABFD, SZ, SZP) \
234   H_PUT_32 (ABFD, (SZ) / bfd_octets_per_byte (ABFD), SZP)
235
236 #define COFF_ADJUST_SCNHDR_IN_POST(ABFD, EXT, INT) \
237   do                                                                    \
238     {                                                                   \
239       ((struct internal_scnhdr *)(INT))->s_page =                       \
240         GET_SCNHDR_PAGE (ABFD, ((SCNHDR *)(EXT))->s_page);              \
241     }                                                                   \
242    while (0)
243
244 /* The line number and reloc overflow checking in coff_swap_scnhdr_out in
245    coffswap.h doesn't use PUT_X for s_nlnno and s_nreloc.
246    Due to different sized v0/v1/v2 section headers, we have to re-write these
247    fields.
248  */
249 #define COFF_ADJUST_SCNHDR_OUT_POST(ABFD, INT, EXT) \
250   do                                                                       \
251     {                                                                      \
252       PUT_SCNHDR_NLNNO (ABFD, ((struct internal_scnhdr *)(INT))->s_nlnno,  \
253                         ((SCNHDR *)(EXT))->s_nlnno);                       \
254       PUT_SCNHDR_NRELOC (ABFD, ((struct internal_scnhdr *)(INT))->s_nreloc,\
255                          ((SCNHDR *)(EXT))->s_nreloc);                     \
256       PUT_SCNHDR_FLAGS (ABFD, ((struct internal_scnhdr *)(INT))->s_flags,  \
257                         ((SCNHDR *)(EXT))->s_flags);                       \
258       PUT_SCNHDR_PAGE (ABFD, ((struct internal_scnhdr *)(INT))->s_page,    \
259                        ((SCNHDR *)(EXT))->s_page);                         \
260     }                                                                      \
261    while (0)
262
263 /*
264  * names of "special" sections
265  */
266 #define _TEXT   ".text"
267 #define _DATA   ".data"
268 #define _BSS    ".bss"
269 #define _CINIT  ".cinit"            /* initialized C data */
270 #define _SCONST  ".const"           /* constants */
271 #define _SWITCH ".switch"           /* switch tables */
272 #define _STACK  ".stack"            /* C stack */
273 #define _SYSMEM ".sysmem"           /* used for malloc et al. syscalls */
274
275 /********************** LINE NUMBERS **********************/
276
277 /* 1 line number entry for every "breakpointable" source line in a section.
278  * Line numbers are grouped on a per function basis; first entry in a function
279  * grouping will have l_lnno = 0 and in place of physical address will be the
280  * symbol table index of the function name.
281  */
282 struct external_lineno {
283   union {
284     char l_symndx[4];   /* function name symbol index, iff l_lnno == 0*/
285     char l_paddr[4];    /* (physical) address of line number    */
286   } l_addr;
287   char l_lnno[2];       /* line number          */
288 };
289
290 #define LINENO  struct external_lineno
291 #define LINESZ  6
292
293
294 /********************** SYMBOLS **********************/
295
296 /* NOTE: this is what a local label looks like in assembly source; what it
297    looks like in COFF output is undefined */
298 #define TICOFF_LOCAL_LABEL_P(NAME) \
299 ((NAME[0] == '$' && NAME[1] >= '0' && NAME[1] <= '9' && NAME[2] == '\0') \
300  || NAME[strlen(NAME)-1] == '?')
301
302 #define E_SYMNMLEN      8       /* # characters in a symbol name        */
303 #define E_FILNMLEN      14      /* # characters in a file name          */
304 #define E_DIMNUM        4       /* # array dimensions in auxiliary entry */
305
306 struct external_syment 
307 {
308   union {
309     char e_name[E_SYMNMLEN];
310     struct {
311       char e_zeroes[4];
312       char e_offset[4];
313     } e;
314   } e;
315   char e_value[4];
316   char e_scnum[2];
317   char e_type[2];
318   char e_sclass[1];
319   char e_numaux[1];
320 };
321
322
323 #define N_BTMASK        (017)
324 #define N_TMASK         (060)
325 #define N_BTSHFT        (4)
326 #define N_TSHIFT        (2)
327   
328
329 union external_auxent {
330   struct {
331         char x_tagndx[4];       /* str, un, or enum tag indx */
332         union {
333           struct {
334                 char  x_lnno[2]; /* declaration line number */
335                 char  x_size[2]; /* str/union/array size */
336           } x_lnsz;
337           char x_fsize[4];      /* size of function */
338         } x_misc;
339         union {
340           struct {              /* if ISFCN, tag, or .bb */
341                 char x_lnnoptr[4];      /* ptr to fcn line # */
342                 char x_endndx[4];       /* entry ndx past block end */
343           } x_fcn;
344           struct {              /* if ISARY, up to 4 dimen. */
345                 char x_dimen[E_DIMNUM][2];
346           } x_ary;
347         } x_fcnary;
348         char x_tvndx[2];                /* tv index */
349   } x_sym;
350   
351   union {
352         char x_fname[E_FILNMLEN];
353         struct {
354           char x_zeroes[4];
355           char x_offset[4];
356         } x_n;
357   } x_file;
358   
359   struct {
360         char x_scnlen[4];                       /* section length */
361         char x_nreloc[2];       /* # relocation entries */
362         char x_nlinno[2];       /* # line numbers */
363   } x_scn;
364   
365   struct {
366         char x_tvfill[4];       /* tv fill value */
367         char x_tvlen[2];        /* length of .tv */
368         char x_tvran[2][2];     /* tv range */
369   } x_tv;               /* info about .tv section (in auxent of symbol .tv)) */
370   
371
372 };
373
374 #define SYMENT  struct external_syment
375 #define SYMESZ  18      
376 #define AUXENT  union external_auxent
377 #define AUXESZ  18
378
379 /* section lengths are in target bytes (not host bytes) */
380 #define GET_SCN_SCNLEN(ABFD, EXT) \
381   (H_GET_32 (ABFD, (EXT)->x_scn.x_scnlen) * bfd_octets_per_byte (ABFD))
382 #define PUT_SCN_SCNLEN(ABFD, INT, EXT) \
383   H_PUT_32 (ABFD, (INT) / bfd_octets_per_byte (ABFD), (EXT)->x_scn.x_scnlen)
384
385 /* lnsz size is in bits in COFF file, in bytes in BFD */
386 #define GET_LNSZ_SIZE(abfd, ext) \
387  (H_GET_16 (abfd, ext->x_sym.x_misc.x_lnsz.x_size) / (class != C_FIELD ? 8 : 1))
388
389 #define PUT_LNSZ_SIZE(abfd, in, ext) \
390   H_PUT_16 (abfd, ((class != C_FIELD) ? (in) * 8 : (in)), \
391            ext->x_sym.x_misc.x_lnsz.x_size)
392  
393 /* TI COFF stores offsets for MOS and MOU in bits; BFD expects bytes 
394    Also put the load page flag of the section into the symbol value if it's an
395    address.  */
396 #ifndef NEEDS_PAGE
397 #define NEEDS_PAGE(X) 0
398 #define PAGE_MASK 0
399 #endif
400 #define COFF_ADJUST_SYM_IN_POST(ABFD, EXT, INT) \
401   do                                                                    \
402     {                                                                   \
403       struct internal_syment *dst = (struct internal_syment *)(INT);    \
404       if (dst->n_sclass == C_MOS || dst->n_sclass == C_MOU)             \
405         dst->n_value /= 8;                                              \
406       else if (NEEDS_PAGE (dst->n_sclass)) {                            \
407         asection *scn = coff_section_from_bfd_index (abfd, dst->n_scnum); \
408         dst->n_value |= (scn->lma & PAGE_MASK);                         \
409       }                                                                 \
410     }                                                                   \
411    while (0)
412
413 #define COFF_ADJUST_SYM_OUT_POST(ABFD, INT, EXT) \
414   do                                                                    \
415     {                                                                   \
416        struct internal_syment *src = (struct internal_syment *)(INT);   \
417        SYMENT *dst = (SYMENT *)(EXT);                                   \
418        if (src->n_sclass == C_MOU || src->n_sclass == C_MOS)            \
419          H_PUT_32 (abfd, src->n_value * 8, dst->e_value);               \
420        else if (NEEDS_PAGE (src->n_sclass)) {                           \
421          H_PUT_32 (abfd, src->n_value &= ~PAGE_MASK, dst->e_value);     \
422        }                                                                \
423     }                                                                   \
424    while (0)
425
426 /* Detect section-relative absolute symbols so they get flagged with a sym
427    index of -1.
428 */
429 #define SECTION_RELATIVE_ABSOLUTE_SYMBOL_P(RELOC, SECT) \
430   ((*(RELOC)->sym_ptr_ptr)->section->output_section == (SECT) \
431    && (RELOC)->howto->name[0] == 'A')
432
433 /********************** RELOCATION DIRECTIVES **********************/
434
435 struct external_reloc_v0
436 {
437   char r_vaddr[4];
438   char r_symndx[2];
439   char r_reserved[2];
440   char r_type[2];
441 };
442
443 struct external_reloc
444 {
445   char r_vaddr[4];
446   char r_symndx[4];
447   char r_reserved[2]; /* extended pmad byte for COFF2 */
448   char r_type[2];
449 };
450
451 #define RELOC struct external_reloc
452 #define RELSZ_V0 10                 /* FIXME -- coffcode.h needs fixing */
453 #define RELSZ 12                    /* for COFF1/2 */
454
455 /* various relocation types.  */
456 #define R_ABS     0x0000            /* no relocation */
457 #define R_REL13   0x002A            /* 13-bit direct reference (???) */
458 #define R_PARTLS7 0x0028            /* 7 LSBs of an address */
459 #define R_PARTMS9 0x0029            /* 9MSBs of an address */
460 #define R_EXTWORD 0x002B            /* 23-bit direct reference */
461 #define R_EXTWORD16 0x002C          /* 16-bit direct reference to 23-bit addr*/
462 #define R_EXTWORDMS7 0x002D         /* upper 7 bits of 23-bit address */
463
464 #endif /* COFF_TI_H */