Commit | Line | Data |
---|---|---|
882bdc69 ILT |
1 | /* tc-ppc.c -- Assemble for the PowerPC or POWER (RS/6000) |
2 | Copyright (C) 1994 Free Software Foundation, Inc. | |
3 | Written by Ian Lance Taylor, Cygnus Support. | |
4 | ||
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
7 | GAS 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, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GAS 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 GAS; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | #include <stdio.h> | |
22 | #include <ctype.h> | |
23 | #include "as.h" | |
24 | #include "subsegs.h" | |
25 | ||
26 | #include "opcode/ppc.h" | |
27 | ||
ce9a2805 MM |
28 | #ifdef OBJ_ELF |
29 | #include "elf/ppc.h" | |
30 | #endif | |
31 | ||
882bdc69 ILT |
32 | /* This is the assembler for the PowerPC or POWER (RS/6000) chips. */ |
33 | ||
34 | /* FIXME: This should be handled in a different way. */ | |
35 | extern int target_big_endian; | |
36 | ||
37 | static void ppc_set_cpu PARAMS ((void)); | |
38 | static unsigned long ppc_insert_operand | |
39 | PARAMS ((unsigned long insn, const struct powerpc_operand *operand, | |
40 | offsetT val, char *file, unsigned int line)); | |
41 | static void ppc_macro PARAMS ((char *str, const struct powerpc_macro *macro)); | |
42 | static void ppc_byte PARAMS ((int)); | |
43 | static int ppc_is_toc_sym PARAMS ((symbolS *sym)); | |
44 | static void ppc_tc PARAMS ((int)); | |
45 | #ifdef OBJ_COFF | |
46 | static void ppc_comm PARAMS ((int)); | |
47 | static void ppc_bb PARAMS ((int)); | |
48 | static void ppc_bf PARAMS ((int)); | |
49 | static void ppc_biei PARAMS ((int)); | |
50 | static void ppc_bs PARAMS ((int)); | |
51 | static void ppc_eb PARAMS ((int)); | |
52 | static void ppc_ef PARAMS ((int)); | |
53 | static void ppc_es PARAMS ((int)); | |
54 | static void ppc_csect PARAMS ((int)); | |
55 | static void ppc_function PARAMS ((int)); | |
56 | static void ppc_extern PARAMS ((int)); | |
57 | static void ppc_lglobl PARAMS ((int)); | |
58 | static void ppc_stabx PARAMS ((int)); | |
59 | static void ppc_rename PARAMS ((int)); | |
60 | static void ppc_toc PARAMS ((int)); | |
61 | #endif | |
4a6b2f8b MM |
62 | #ifdef OBJ_ELF |
63 | static bfd_reloc_code_real_type ppc_elf_suffix PARAMS ((char **)); | |
64 | static void ppc_elf_cons PARAMS ((int)); | |
3f81f3cf | 65 | static void ppc_elf_validate_fix (fixS *, segT); |
4a6b2f8b | 66 | #endif |
882bdc69 ILT |
67 | \f |
68 | /* Generic assembler global variables which must be defined by all | |
69 | targets. */ | |
70 | ||
71 | /* Characters which always start a comment. */ | |
72 | const char comment_chars[] = "#"; | |
73 | ||
74 | /* Characters which start a comment at the beginning of a line. */ | |
75 | const char line_comment_chars[] = "#"; | |
76 | ||
77 | /* Characters which may be used to separate multiple commands on a | |
78 | single line. */ | |
79 | const char line_separator_chars[] = ";"; | |
80 | ||
81 | /* Characters which are used to indicate an exponent in a floating | |
82 | point number. */ | |
83 | const char EXP_CHARS[] = "eE"; | |
84 | ||
85 | /* Characters which mean that a number is a floating point constant, | |
86 | as in 0d1.0. */ | |
87 | const char FLT_CHARS[] = "dD"; | |
88 | \f | |
89 | /* The target specific pseudo-ops which we support. */ | |
90 | ||
91 | const pseudo_typeS md_pseudo_table[] = | |
92 | { | |
93 | /* Pseudo-ops which must be overridden. */ | |
94 | { "byte", ppc_byte, 0 }, | |
95 | ||
96 | #ifdef OBJ_COFF | |
97 | /* Pseudo-ops specific to the RS/6000 XCOFF format. Some of these | |
98 | legitimately belong in the obj-*.c file. However, XCOFF is based | |
99 | on COFF, and is only implemented for the RS/6000. We just use | |
100 | obj-coff.c, and add what we need here. */ | |
101 | { "comm", ppc_comm, 0 }, | |
102 | { "lcomm", ppc_comm, 1 }, | |
103 | { "bb", ppc_bb, 0 }, | |
104 | { "bf", ppc_bf, 0 }, | |
105 | { "bi", ppc_biei, 0 }, | |
106 | { "bs", ppc_bs, 0 }, | |
107 | { "csect", ppc_csect, 0 }, | |
108 | { "eb", ppc_eb, 0 }, | |
109 | { "ef", ppc_ef, 0 }, | |
110 | { "ei", ppc_biei, 1 }, | |
111 | { "es", ppc_es, 0 }, | |
112 | { "extern", ppc_extern, 0 }, | |
113 | { "function", ppc_function, 0 }, | |
114 | { "lglobl", ppc_lglobl, 0 }, | |
115 | { "rename", ppc_rename, 0 }, | |
116 | { "stabx", ppc_stabx, 0 }, | |
117 | { "toc", ppc_toc, 0 }, | |
118 | #endif | |
4a6b2f8b MM |
119 | #ifdef OBJ_ELF |
120 | { "long", ppc_elf_cons, 4 }, | |
121 | { "word", ppc_elf_cons, 2 }, | |
122 | { "short", ppc_elf_cons, 2 }, | |
4a6b2f8b | 123 | #endif |
882bdc69 ILT |
124 | |
125 | /* This pseudo-op is used even when not generating XCOFF output. */ | |
126 | { "tc", ppc_tc, 0 }, | |
127 | ||
128 | { NULL, NULL, 0 } | |
129 | }; | |
130 | \f | |
131 | /* Local variables. */ | |
132 | ||
1e147242 ILT |
133 | /* The type of processor we are assembling for. This is one or more |
134 | of the PPC_OPCODE flags defined in opcode/ppc.h. */ | |
882bdc69 ILT |
135 | static int ppc_cpu = 0; |
136 | ||
1e147242 ILT |
137 | /* The size of the processor we are assembling for. This is either |
138 | PPC_OPCODE_32 or PPC_OPCODE_64. */ | |
139 | static int ppc_size = PPC_OPCODE_32; | |
140 | ||
882bdc69 ILT |
141 | /* The endianness we are using. */ |
142 | static int ppc_big_endian = PPC_BIG_ENDIAN; | |
143 | ||
144 | /* Opcode hash table. */ | |
145 | static struct hash_control *ppc_hash; | |
146 | ||
147 | /* Macro hash table. */ | |
148 | static struct hash_control *ppc_macro_hash; | |
149 | ||
3f81f3cf MM |
150 | #ifdef OBJ_ELF |
151 | /* Whether to warn about non PC relative relocations that aren't | |
152 | in the .got2 section. */ | |
ce9a2805 | 153 | static boolean mrelocatable = false; |
3f81f3cf MM |
154 | #endif |
155 | ||
882bdc69 ILT |
156 | #ifdef OBJ_COFF |
157 | ||
158 | /* The RS/6000 assembler uses the .csect pseudo-op to generate code | |
159 | using a bunch of different sections. These assembler sections, | |
160 | however, are all encompassed within the .text or .data sections of | |
161 | the final output file. We handle this by using different | |
162 | subsegments within these main segments. */ | |
163 | ||
164 | /* Next subsegment to allocate within the .text segment. */ | |
165 | static subsegT ppc_text_subsegment = 2; | |
166 | ||
167 | /* Linked list of csects in the text section. */ | |
168 | static symbolS *ppc_text_csects; | |
169 | ||
170 | /* Next subsegment to allocate within the .data segment. */ | |
171 | static subsegT ppc_data_subsegment = 2; | |
172 | ||
173 | /* Linked list of csects in the data section. */ | |
174 | static symbolS *ppc_data_csects; | |
175 | ||
176 | /* The current csect. */ | |
177 | static symbolS *ppc_current_csect; | |
178 | ||
179 | /* The RS/6000 assembler uses a TOC which holds addresses of functions | |
180 | and variables. Symbols are put in the TOC with the .tc pseudo-op. | |
181 | A special relocation is used when accessing TOC entries. We handle | |
182 | the TOC as a subsegment within the .data segment. We set it up if | |
183 | we see a .toc pseudo-op, and save the csect symbol here. */ | |
184 | static symbolS *ppc_toc_csect; | |
185 | ||
186 | /* The first frag in the TOC subsegment. */ | |
187 | static fragS *ppc_toc_frag; | |
188 | ||
189 | /* The first frag in the first subsegment after the TOC in the .data | |
190 | segment. NULL if there are no subsegments after the TOC. */ | |
191 | static fragS *ppc_after_toc_frag; | |
192 | ||
1eeb357e ILT |
193 | /* The current static block. */ |
194 | static symbolS *ppc_current_block; | |
195 | ||
882bdc69 ILT |
196 | /* The COFF debugging section; set by md_begin. This is not the |
197 | .debug section, but is instead the secret BFD section which will | |
198 | cause BFD to set the section number of a symbol to N_DEBUG. */ | |
199 | static asection *ppc_coff_debug_section; | |
200 | ||
201 | /* The size of the .debug section. */ | |
202 | static bfd_size_type ppc_debug_name_section_size; | |
203 | ||
204 | #endif /* OBJ_COFF */ | |
da8fa3ba MM |
205 | |
206 | #ifdef OBJ_ELF | |
207 | symbolS *GOT_symbol; /* Pre-defined "_GLOBAL_OFFSET_TABLE" */ | |
208 | #endif /* OBJ_ELF */ | |
209 | ||
4a6b2f8b MM |
210 | #ifndef WORKING_DOT_WORD |
211 | const int md_short_jump_size = 4; | |
212 | const int md_long_jump_size = 4; | |
213 | #endif | |
882bdc69 | 214 | \f |
1eeb357e ILT |
215 | #ifdef OBJ_ELF |
216 | CONST char *md_shortopts = "um:VQ:"; | |
217 | #else | |
218 | CONST char *md_shortopts = "um:"; | |
219 | #endif | |
220 | struct option md_longopts[] = { | |
221 | {NULL, no_argument, NULL, 0} | |
222 | }; | |
223 | size_t md_longopts_size = sizeof(md_longopts); | |
882bdc69 ILT |
224 | |
225 | int | |
1eeb357e ILT |
226 | md_parse_option (c, arg) |
227 | int c; | |
228 | char *arg; | |
882bdc69 | 229 | { |
1eeb357e | 230 | switch (c) |
882bdc69 | 231 | { |
1eeb357e ILT |
232 | case 'u': |
233 | /* -u means that any undefined symbols should be treated as | |
234 | external, which is the default for gas anyhow. */ | |
235 | break; | |
882bdc69 | 236 | |
1eeb357e ILT |
237 | case 'm': |
238 | /* -mpwrx and -mpwr2 mean to assemble for the IBM POWER/2 | |
239 | (RIOS2). */ | |
240 | if (strcmp (arg, "pwrx") == 0 || strcmp (arg, "pwr2") == 0) | |
241 | ppc_cpu = PPC_OPCODE_POWER | PPC_OPCODE_POWER2; | |
242 | /* -mpwr means to assemble for the IBM POWER (RIOS1). */ | |
243 | else if (strcmp (arg, "pwr") == 0) | |
244 | ppc_cpu = PPC_OPCODE_POWER; | |
245 | /* -m601 means to assemble for the Motorola PowerPC 601. FIXME: We | |
246 | ignore the option for now, but we should really use it to permit | |
247 | instructions defined on the 601 that are not part of the standard | |
248 | PowerPC architecture (mostly holdovers from the POWER). */ | |
249 | else if (strcmp (arg, "601") == 0) | |
250 | ppc_cpu = PPC_OPCODE_PPC | PPC_OPCODE_601; | |
251 | /* -mppc, -mppc32, -m603, and -m604 mean to assemble for the | |
252 | Motorola PowerPC 603/604. */ | |
253 | else if (strcmp (arg, "ppc") == 0 | |
254 | || strcmp (arg, "ppc32") == 0 | |
9b0da28b | 255 | || strcmp (arg, "403") == 0 |
1eeb357e ILT |
256 | || strcmp (arg, "603") == 0 |
257 | || strcmp (arg, "604") == 0) | |
258 | ppc_cpu = PPC_OPCODE_PPC; | |
259 | /* -mppc64 and -m620 mean to assemble for the 64-bit PowerPC | |
260 | 620. */ | |
261 | else if (strcmp (arg, "ppc64") == 0 || strcmp (arg, "620") == 0) | |
262 | { | |
263 | ppc_cpu = PPC_OPCODE_PPC; | |
264 | ppc_size = PPC_OPCODE_64; | |
265 | } | |
266 | /* -many means to assemble for any architecture (PWR/PWRX/PPC). */ | |
267 | else if (strcmp (arg, "any") == 0) | |
268 | ppc_cpu = PPC_OPCODE_POWER | PPC_OPCODE_POWER2 | PPC_OPCODE_PPC; | |
3f81f3cf MM |
269 | #ifdef OBJ_ELF |
270 | /* -mrelocatable -- warn about initializations that require relocation */ | |
271 | else if (strcmp (arg, "relocatable") == 0) | |
ce9a2805 | 272 | mrelocatable = true; |
3f81f3cf | 273 | #endif |
1eeb357e ILT |
274 | else |
275 | { | |
276 | as_bad ("invalid architecture -m%s", arg); | |
277 | return 0; | |
278 | } | |
279 | break; | |
882bdc69 | 280 | |
1eeb357e ILT |
281 | #ifdef OBJ_ELF |
282 | /* -V: SVR4 argument to print version ID. */ | |
283 | case 'V': | |
284 | print_version_id (); | |
285 | break; | |
882bdc69 | 286 | |
1eeb357e ILT |
287 | /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section |
288 | should be emitted or not. FIXME: Not implemented. */ | |
289 | case 'Q': | |
290 | break; | |
291 | #endif | |
882bdc69 | 292 | |
1eeb357e ILT |
293 | default: |
294 | return 0; | |
882bdc69 ILT |
295 | } |
296 | ||
1eeb357e ILT |
297 | return 1; |
298 | } | |
882bdc69 | 299 | |
1eeb357e ILT |
300 | void |
301 | md_show_usage (stream) | |
302 | FILE *stream; | |
303 | { | |
304 | fprintf(stream, "\ | |
305 | PowerPC options:\n\ | |
306 | -u ignored\n\ | |
307 | -mpwrx generate code for IBM POWER/2 (RIOS2)\n\ | |
308 | -mpwr generate code for IBM POWER (RIOS1)\n\ | |
309 | -m601 generate code for Motorola PowerPC 601\n\ | |
9b0da28b | 310 | -mppc, -mppc32, -m403, -m603, -m604\n\ |
4a6b2f8b | 311 | generate code for Motorola PowerPC 603/604\n\ |
1eeb357e | 312 | -many generate code for any architecture (PWR/PWRX/PPC)\n"); |
882bdc69 | 313 | #ifdef OBJ_ELF |
1eeb357e | 314 | fprintf(stream, "\ |
3f81f3cf | 315 | -mrelocatable warn incompatible with GCC's -mrelocatble option\n\ |
1eeb357e ILT |
316 | -V print assembler version number\n\ |
317 | -Qy, -Qn ignored\n"); | |
882bdc69 | 318 | #endif |
882bdc69 | 319 | } |
1eeb357e | 320 | \f |
882bdc69 ILT |
321 | /* Set ppc_cpu if it is not already set. */ |
322 | ||
323 | static void | |
324 | ppc_set_cpu () | |
325 | { | |
326 | const char *default_cpu = TARGET_CPU; | |
327 | ||
328 | if (ppc_cpu == 0) | |
329 | { | |
330 | if (strcmp (default_cpu, "rs6000") == 0) | |
331 | ppc_cpu = PPC_OPCODE_POWER; | |
332 | else if (strcmp (default_cpu, "powerpc") == 0) | |
333 | ppc_cpu = PPC_OPCODE_PPC; | |
334 | else | |
335 | abort (); | |
336 | } | |
337 | } | |
338 | ||
339 | /* Figure out the BFD architecture to use. */ | |
340 | ||
341 | enum bfd_architecture | |
342 | ppc_arch () | |
343 | { | |
344 | ppc_set_cpu (); | |
345 | ||
1e147242 | 346 | if ((ppc_cpu & PPC_OPCODE_PPC) != 0) |
882bdc69 | 347 | return bfd_arch_powerpc; |
1e147242 ILT |
348 | else if ((ppc_cpu & PPC_OPCODE_POWER) != 0) |
349 | return bfd_arch_rs6000; | |
882bdc69 ILT |
350 | else |
351 | abort (); | |
352 | } | |
353 | ||
354 | /* This function is called when the assembler starts up. It is called | |
355 | after the options have been parsed and the output file has been | |
356 | opened. */ | |
357 | ||
358 | void | |
359 | md_begin () | |
360 | { | |
361 | register const struct powerpc_opcode *op; | |
362 | const struct powerpc_opcode *op_end; | |
363 | const struct powerpc_macro *macro; | |
364 | const struct powerpc_macro *macro_end; | |
365 | ||
366 | ppc_set_cpu (); | |
367 | ||
ce9a2805 MM |
368 | #ifdef OBJ_ELF |
369 | /* Set the -mrelocatable flag bit */ | |
370 | if (mrelocatable) | |
371 | bfd_set_private_flags (stdoutput, EF_PPC_RELOCATABLE); | |
372 | #endif | |
373 | ||
882bdc69 ILT |
374 | /* Insert the opcodes into a hash table. */ |
375 | ppc_hash = hash_new (); | |
376 | ||
377 | op_end = powerpc_opcodes + powerpc_num_opcodes; | |
378 | for (op = powerpc_opcodes; op < op_end; op++) | |
379 | { | |
380 | know ((op->opcode & op->mask) == op->opcode); | |
381 | ||
1e147242 ILT |
382 | if ((op->flags & ppc_cpu) != 0 |
383 | && ((op->flags & (PPC_OPCODE_32 | PPC_OPCODE_64)) == 0 | |
384 | || (op->flags & (PPC_OPCODE_32 | PPC_OPCODE_64)) == ppc_size)) | |
882bdc69 ILT |
385 | { |
386 | const char *retval; | |
387 | ||
388 | retval = hash_insert (ppc_hash, op->name, (PTR) op); | |
389 | if (retval != (const char *) NULL) | |
1eeb357e | 390 | { |
76e30835 ILT |
391 | /* We permit a duplication of the mfdec instruction on |
392 | the 601, because it seems to have one value on the | |
393 | 601 and a different value on other PowerPC | |
394 | processors. It's easier to permit a duplication than | |
395 | to define a new instruction type flag. When using | |
396 | -many, the comparison instructions are a harmless | |
397 | special case. */ | |
398 | if (strcmp (retval, "exists") != 0 | |
399 | || (((ppc_cpu & PPC_OPCODE_601) == 0 | |
400 | || strcmp (op->name, "mfdec") != 0) | |
401 | && (ppc_cpu != (PPC_OPCODE_POWER | |
402 | | PPC_OPCODE_POWER2 | |
403 | | PPC_OPCODE_PPC) | |
404 | || (strcmp (op->name, "cmpli") != 0 | |
405 | && strcmp (op->name, "cmpi") != 0 | |
406 | && strcmp (op->name, "cmp") != 0 | |
407 | && strcmp (op->name, "cmpl") != 0)))) | |
1eeb357e ILT |
408 | abort (); |
409 | } | |
882bdc69 ILT |
410 | } |
411 | } | |
412 | ||
413 | /* Insert the macros into a hash table. */ | |
414 | ppc_macro_hash = hash_new (); | |
415 | ||
416 | macro_end = powerpc_macros + powerpc_num_macros; | |
417 | for (macro = powerpc_macros; macro < macro_end; macro++) | |
418 | { | |
419 | if ((macro->flags & ppc_cpu) != 0) | |
420 | { | |
421 | const char *retval; | |
422 | ||
423 | retval = hash_insert (ppc_macro_hash, macro->name, (PTR) macro); | |
424 | if (retval != (const char *) NULL) | |
425 | abort (); | |
426 | } | |
427 | } | |
428 | ||
429 | /* Tell the main code what the endianness is. */ | |
430 | target_big_endian = ppc_big_endian; | |
431 | ||
432 | #ifdef OBJ_COFF | |
433 | ppc_coff_debug_section = coff_section_from_bfd_index (stdoutput, N_DEBUG); | |
434 | ||
435 | /* Create dummy symbols to serve as initial csects. This forces the | |
436 | text csects to precede the data csects. These symbols will not | |
437 | be output. */ | |
438 | ppc_text_csects = symbol_make ("dummy\001"); | |
439 | ppc_text_csects->sy_tc.within = ppc_text_csects; | |
440 | ppc_data_csects = symbol_make ("dummy\001"); | |
441 | ppc_data_csects->sy_tc.within = ppc_data_csects; | |
442 | #endif | |
443 | } | |
444 | ||
445 | /* Insert an operand value into an instruction. */ | |
446 | ||
447 | static unsigned long | |
448 | ppc_insert_operand (insn, operand, val, file, line) | |
449 | unsigned long insn; | |
450 | const struct powerpc_operand *operand; | |
451 | offsetT val; | |
452 | char *file; | |
453 | unsigned int line; | |
454 | { | |
455 | if (operand->bits != 32) | |
456 | { | |
457 | long min, max; | |
458 | offsetT test; | |
459 | ||
1eeb357e | 460 | if ((operand->flags & PPC_OPERAND_SIGNED) != 0) |
882bdc69 | 461 | { |
1eeb357e ILT |
462 | if ((operand->flags & PPC_OPERAND_SIGNOPT) != 0 |
463 | && ppc_size == PPC_OPCODE_32) | |
464 | max = (1 << operand->bits) - 1; | |
465 | else | |
466 | max = (1 << (operand->bits - 1)) - 1; | |
882bdc69 ILT |
467 | min = - (1 << (operand->bits - 1)); |
468 | } | |
469 | else | |
470 | { | |
471 | max = (1 << operand->bits) - 1; | |
472 | min = 0; | |
473 | } | |
474 | ||
475 | if ((operand->flags & PPC_OPERAND_NEGATIVE) != 0) | |
476 | test = - val; | |
477 | else | |
478 | test = val; | |
479 | ||
480 | if (test < (offsetT) min || test > (offsetT) max) | |
481 | { | |
482 | const char *err = | |
483 | "operand out of range (%s not between %ld and %ld)"; | |
484 | char buf[100]; | |
485 | ||
486 | sprint_value (buf, test); | |
487 | if (file == (char *) NULL) | |
488 | as_warn (err, buf, min, max); | |
489 | else | |
490 | as_warn_where (file, line, err, buf, min, max); | |
491 | } | |
492 | } | |
493 | ||
494 | if (operand->insert) | |
495 | { | |
496 | const char *errmsg; | |
497 | ||
498 | errmsg = NULL; | |
499 | insn = (*operand->insert) (insn, (long) val, &errmsg); | |
500 | if (errmsg != (const char *) NULL) | |
501 | as_warn (errmsg); | |
502 | } | |
503 | else | |
504 | insn |= (((long) val & ((1 << operand->bits) - 1)) | |
505 | << operand->shift); | |
506 | ||
507 | return insn; | |
508 | } | |
509 | ||
4a6b2f8b MM |
510 | #ifdef OBJ_ELF |
511 | /* Parse @got, etc. and return the desired relocation. */ | |
512 | static bfd_reloc_code_real_type | |
513 | ppc_elf_suffix (str_p) | |
514 | char **str_p; | |
515 | { | |
516 | char *str = *str_p; | |
517 | ||
518 | if (*str != '@') | |
519 | return BFD_RELOC_UNUSED; | |
520 | ||
521 | if (strncmp (str, "@GOT", 4) == 0 || strncmp (str, "@got", 4) == 0) | |
522 | { | |
523 | *str_p += 4; | |
524 | return BFD_RELOC_PPC_TOC16; | |
525 | } | |
526 | else if (strncmp (str, "@L", 2) == 0 || strncmp (str, "@l", 2) == 0) | |
527 | { | |
528 | *str_p += 2; | |
529 | return BFD_RELOC_LO16; | |
530 | } | |
531 | else if (strncmp (str, "@HA", 3) == 0 || strncmp (str, "@ha", 3) == 0) | |
532 | { | |
533 | *str_p += 3; | |
534 | return BFD_RELOC_HI16_S; | |
535 | } | |
536 | else if (strncmp (str, "@H", 2) == 0 || strncmp (str, "@h", 2) == 0) | |
537 | { | |
538 | *str_p += 2; | |
539 | return BFD_RELOC_HI16; | |
540 | } | |
4a6b2f8b MM |
541 | |
542 | return BFD_RELOC_UNUSED; | |
543 | } | |
544 | ||
3f81f3cf | 545 | /* Like normal .long/.short/.word, except support @got, etc. */ |
4a6b2f8b MM |
546 | /* clobbers input_line_pointer, checks */ |
547 | /* end-of-line. */ | |
548 | static void | |
549 | ppc_elf_cons (nbytes) | |
550 | register int nbytes; /* 1=.byte, 2=.word, 4=.long */ | |
551 | { | |
552 | expressionS exp; | |
553 | bfd_reloc_code_real_type reloc; | |
554 | ||
555 | if (is_it_end_of_statement ()) | |
556 | { | |
557 | demand_empty_rest_of_line (); | |
558 | return; | |
559 | } | |
560 | ||
561 | do | |
562 | { | |
563 | expression (&exp); | |
564 | if (nbytes == 4 | |
565 | && exp.X_op == O_symbol | |
566 | && *input_line_pointer == '@' | |
567 | && (reloc = ppc_elf_suffix (&input_line_pointer)) != BFD_RELOC_UNUSED) | |
568 | { | |
569 | register char *p = frag_more ((int) nbytes); | |
570 | reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, reloc); | |
571 | int offset = (!reloc_howto) ? 0 : (nbytes - bfd_get_reloc_size (reloc_howto)); | |
572 | ||
573 | if (offset < 0) | |
574 | offset = 0; | |
575 | ||
576 | fix_new_exp (frag_now, p - frag_now->fr_literal + offset, (int) nbytes - offset, &exp, 0, reloc); | |
577 | } | |
578 | else | |
579 | emit_expr (&exp, (unsigned int) nbytes); | |
580 | } | |
581 | while (*input_line_pointer++ == ','); | |
582 | ||
583 | input_line_pointer--; /* Put terminator back into stream. */ | |
584 | demand_empty_rest_of_line (); | |
585 | } | |
586 | ||
3f81f3cf MM |
587 | /* Validate any relocations emitted for -mrelocatable */ |
588 | static void | |
589 | ppc_elf_validate_fix (fixS *fixp, segT seg) | |
590 | { | |
591 | if (mrelocatable | |
592 | && !fixp->fx_done | |
593 | && !fixp->fx_pcrel | |
594 | && fixp->fx_r_type <= BFD_RELOC_UNUSED | |
74e1b52e | 595 | && strcmp (segment_name (seg), ".got2") != 0 |
9b0da28b KR |
596 | && strcmp (segment_name (seg), ".dtors") != 0 |
597 | && strcmp (segment_name (seg), ".ctors") != 0 | |
74e1b52e | 598 | && strcmp (segment_name (seg), ".stab") != 0) |
3f81f3cf | 599 | { |
ce9a2805 MM |
600 | as_warn_where (fixp->fx_file, fixp->fx_line, |
601 | "Relocation cannot be done when using -mrelocatable"); | |
3f81f3cf MM |
602 | } |
603 | } | |
604 | ||
4a6b2f8b MM |
605 | #endif /* OBJ_ELF */ |
606 | ||
882bdc69 ILT |
607 | /* We need to keep a list of fixups. We can't simply generate them as |
608 | we go, because that would require us to first create the frag, and | |
609 | that would screw up references to ``.''. */ | |
610 | ||
611 | struct ppc_fixup | |
612 | { | |
613 | expressionS exp; | |
614 | int opindex; | |
4a6b2f8b | 615 | bfd_reloc_code_real_type reloc; |
882bdc69 ILT |
616 | }; |
617 | ||
618 | #define MAX_INSN_FIXUPS (5) | |
619 | ||
620 | /* This routine is called for each instruction to be assembled. */ | |
621 | ||
622 | void | |
623 | md_assemble (str) | |
624 | char *str; | |
625 | { | |
626 | char *s; | |
627 | const struct powerpc_opcode *opcode; | |
628 | unsigned long insn; | |
629 | const unsigned char *opindex_ptr; | |
630 | int skip_optional; | |
631 | int need_paren; | |
632 | int next_opindex; | |
633 | struct ppc_fixup fixups[MAX_INSN_FIXUPS]; | |
634 | int fc; | |
635 | char *f; | |
636 | int i; | |
4a6b2f8b | 637 | bfd_reloc_code_real_type reloc; |
882bdc69 ILT |
638 | |
639 | /* Get the opcode. */ | |
640 | for (s = str; *s != '\0' && ! isspace (*s); s++) | |
641 | ; | |
642 | if (*s != '\0') | |
643 | *s++ = '\0'; | |
644 | ||
645 | /* Look up the opcode in the hash table. */ | |
646 | opcode = (const struct powerpc_opcode *) hash_find (ppc_hash, str); | |
647 | if (opcode == (const struct powerpc_opcode *) NULL) | |
648 | { | |
649 | const struct powerpc_macro *macro; | |
650 | ||
651 | macro = (const struct powerpc_macro *) hash_find (ppc_macro_hash, str); | |
652 | if (macro == (const struct powerpc_macro *) NULL) | |
653 | as_bad ("Unrecognized opcode: `%s'", str); | |
654 | else | |
655 | ppc_macro (s, macro); | |
656 | ||
657 | return; | |
658 | } | |
659 | ||
660 | insn = opcode->opcode; | |
661 | ||
662 | str = s; | |
663 | while (isspace (*str)) | |
664 | ++str; | |
665 | ||
666 | /* PowerPC operands are just expressions. The only real issue is | |
667 | that a few operand types are optional. All cases which might use | |
668 | an optional operand separate the operands only with commas (in | |
669 | some cases parentheses are used, as in ``lwz 1,0(1)'' but such | |
670 | cases never have optional operands). There is never more than | |
671 | one optional operand for an instruction. So, before we start | |
672 | seriously parsing the operands, we check to see if we have an | |
673 | optional operand, and, if we do, we count the number of commas to | |
674 | see whether the operand should be omitted. */ | |
675 | skip_optional = 0; | |
676 | for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++) | |
677 | { | |
678 | const struct powerpc_operand *operand; | |
679 | ||
680 | operand = &powerpc_operands[*opindex_ptr]; | |
681 | if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0) | |
682 | { | |
683 | unsigned int opcount; | |
684 | ||
685 | /* There is an optional operand. Count the number of | |
686 | commas in the input line. */ | |
687 | if (*str == '\0') | |
688 | opcount = 0; | |
689 | else | |
690 | { | |
691 | opcount = 1; | |
692 | s = str; | |
693 | while ((s = strchr (s, ',')) != (char *) NULL) | |
694 | { | |
695 | ++opcount; | |
696 | ++s; | |
697 | } | |
698 | } | |
699 | ||
700 | /* If there are fewer operands in the line then are called | |
701 | for by the instruction, we want to skip the optional | |
702 | operand. */ | |
703 | if (opcount < strlen (opcode->operands)) | |
704 | skip_optional = 1; | |
705 | ||
706 | break; | |
707 | } | |
708 | } | |
709 | ||
710 | /* Gather the operands. */ | |
711 | need_paren = 0; | |
712 | next_opindex = 0; | |
713 | fc = 0; | |
714 | for (opindex_ptr = opcode->operands; *opindex_ptr != 0; opindex_ptr++) | |
715 | { | |
716 | const struct powerpc_operand *operand; | |
717 | const char *errmsg; | |
718 | char *hold; | |
719 | expressionS ex; | |
720 | char endc; | |
721 | ||
722 | if (next_opindex == 0) | |
723 | operand = &powerpc_operands[*opindex_ptr]; | |
724 | else | |
725 | { | |
726 | operand = &powerpc_operands[next_opindex]; | |
727 | next_opindex = 0; | |
728 | } | |
729 | ||
730 | errmsg = NULL; | |
731 | ||
732 | /* If this is a fake operand, then we do not expect anything | |
733 | from the input. */ | |
734 | if ((operand->flags & PPC_OPERAND_FAKE) != 0) | |
735 | { | |
736 | insn = (*operand->insert) (insn, 0L, &errmsg); | |
737 | if (errmsg != (const char *) NULL) | |
738 | as_warn (errmsg); | |
739 | continue; | |
740 | } | |
741 | ||
742 | /* If this is an optional operand, and we are skipping it, just | |
743 | insert a zero. */ | |
744 | if ((operand->flags & PPC_OPERAND_OPTIONAL) != 0 | |
745 | && skip_optional) | |
746 | { | |
747 | if (operand->insert) | |
748 | { | |
749 | insn = (*operand->insert) (insn, 0L, &errmsg); | |
750 | if (errmsg != (const char *) NULL) | |
751 | as_warn (errmsg); | |
752 | } | |
753 | if ((operand->flags & PPC_OPERAND_NEXT) != 0) | |
754 | next_opindex = *opindex_ptr + 1; | |
755 | continue; | |
756 | } | |
757 | ||
758 | /* Gather the operand. */ | |
759 | hold = input_line_pointer; | |
760 | input_line_pointer = str; | |
761 | expression (&ex); | |
762 | str = input_line_pointer; | |
763 | input_line_pointer = hold; | |
764 | ||
765 | if (ex.X_op == O_illegal) | |
766 | as_bad ("illegal operand"); | |
767 | else if (ex.X_op == O_absent) | |
768 | as_bad ("missing operand"); | |
769 | else if (ex.X_op == O_constant) | |
770 | insn = ppc_insert_operand (insn, operand, ex.X_add_number, | |
771 | (char *) NULL, 0); | |
4a6b2f8b MM |
772 | |
773 | #ifdef OBJ_ELF | |
774 | else if ((reloc = ppc_elf_suffix (&str)) != BFD_RELOC_UNUSED) | |
775 | { | |
776 | /* We need to generate a fixup for this expression. */ | |
777 | if (fc >= MAX_INSN_FIXUPS) | |
778 | as_fatal ("too many fixups"); | |
779 | fixups[fc].exp = ex; | |
780 | fixups[fc].opindex = 0; | |
781 | fixups[fc].reloc = reloc; | |
782 | ++fc; | |
783 | } | |
784 | #endif /* OBJ_ELF */ | |
785 | ||
882bdc69 ILT |
786 | else |
787 | { | |
788 | /* We need to generate a fixup for this expression. */ | |
789 | if (fc >= MAX_INSN_FIXUPS) | |
790 | as_fatal ("too many fixups"); | |
791 | fixups[fc].exp = ex; | |
792 | fixups[fc].opindex = *opindex_ptr; | |
4a6b2f8b | 793 | fixups[fc].reloc = BFD_RELOC_UNUSED; |
882bdc69 ILT |
794 | ++fc; |
795 | } | |
796 | ||
797 | if (need_paren) | |
798 | { | |
799 | endc = ')'; | |
800 | need_paren = 0; | |
801 | } | |
802 | else if ((operand->flags & PPC_OPERAND_PARENS) != 0) | |
803 | { | |
804 | endc = '('; | |
805 | need_paren = 1; | |
806 | } | |
807 | else | |
808 | endc = ','; | |
809 | ||
810 | /* The call to expression should have advanced str past any | |
811 | whitespace. */ | |
812 | if (*str != endc | |
813 | && (endc != ',' || *str != '\0')) | |
814 | { | |
815 | as_bad ("syntax error; found `%c' but expected `%c'", *str, endc); | |
816 | break; | |
817 | } | |
818 | ||
819 | if (*str != '\0') | |
820 | ++str; | |
821 | } | |
822 | ||
823 | while (isspace (*str)) | |
824 | ++str; | |
825 | ||
826 | if (*str != '\0') | |
827 | as_bad ("junk at end of line: `%s'", str); | |
828 | ||
829 | /* Write out the instruction. */ | |
830 | f = frag_more (4); | |
831 | md_number_to_chars (f, insn, 4); | |
832 | ||
833 | /* Create any fixups. At this point we do not use a | |
834 | bfd_reloc_code_real_type, but instead just use the operand index. | |
835 | This lets us easily handle fixups for any operand type, although | |
836 | that is admittedly not a very exciting feature. We pick a BFD | |
837 | reloc type in md_apply_fix. */ | |
838 | for (i = 0; i < fc; i++) | |
839 | { | |
840 | const struct powerpc_operand *operand; | |
841 | ||
842 | operand = &powerpc_operands[fixups[i].opindex]; | |
4a6b2f8b MM |
843 | if (fixups[i].reloc != BFD_RELOC_UNUSED) |
844 | { | |
845 | reloc_howto_type *reloc_howto = bfd_reloc_type_lookup (stdoutput, fixups[i].reloc); | |
846 | int offset = (!reloc_howto) ? 0 : (4 - bfd_get_reloc_size (reloc_howto)); | |
847 | ||
848 | if (offset < 0) | |
849 | offset = 0; | |
850 | ||
851 | fix_new_exp (frag_now, f - frag_now->fr_literal + offset, 4 - offset, | |
852 | &fixups[i].exp, (reloc_howto && reloc_howto->pc_relative), | |
853 | fixups[i].reloc); | |
854 | } | |
855 | else | |
856 | fix_new_exp (frag_now, f - frag_now->fr_literal, 4, | |
857 | &fixups[i].exp, | |
858 | (operand->flags & PPC_OPERAND_RELATIVE) != 0, | |
859 | ((bfd_reloc_code_real_type) | |
860 | (fixups[i].opindex + (int) BFD_RELOC_UNUSED))); | |
882bdc69 ILT |
861 | } |
862 | } | |
863 | ||
4a6b2f8b MM |
864 | #ifndef WORKING_DOT_WORD |
865 | /* Handle long and short jumps */ | |
866 | void | |
867 | md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol) | |
868 | char *ptr; | |
869 | addressT from_addr, to_addr; | |
870 | fragS *frag; | |
871 | symbolS *to_symbol; | |
872 | { | |
873 | abort (); | |
874 | } | |
875 | ||
876 | void | |
877 | md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol) | |
878 | char *ptr; | |
879 | addressT from_addr, to_addr; | |
880 | fragS *frag; | |
881 | symbolS *to_symbol; | |
882 | { | |
883 | abort (); | |
884 | } | |
885 | #endif | |
886 | ||
882bdc69 ILT |
887 | /* Handle a macro. Gather all the operands, transform them as |
888 | described by the macro, and call md_assemble recursively. All the | |
889 | operands are separated by commas; we don't accept parentheses | |
890 | around operands here. */ | |
891 | ||
892 | static void | |
893 | ppc_macro (str, macro) | |
894 | char *str; | |
895 | const struct powerpc_macro *macro; | |
896 | { | |
897 | char *operands[10]; | |
898 | int count; | |
899 | char *s; | |
900 | unsigned int len; | |
901 | const char *format; | |
902 | int arg; | |
903 | char *send; | |
904 | char *complete; | |
905 | ||
906 | /* Gather the users operands into the operands array. */ | |
907 | count = 0; | |
908 | s = str; | |
909 | while (1) | |
910 | { | |
911 | if (count >= sizeof operands / sizeof operands[0]) | |
912 | break; | |
913 | operands[count++] = s; | |
914 | s = strchr (s, ','); | |
915 | if (s == (char *) NULL) | |
916 | break; | |
917 | *s++ = '\0'; | |
918 | } | |
919 | ||
920 | if (count != macro->operands) | |
921 | { | |
922 | as_bad ("wrong number of operands"); | |
923 | return; | |
924 | } | |
925 | ||
926 | /* Work out how large the string must be (the size is unbounded | |
927 | because it includes user input). */ | |
928 | len = 0; | |
929 | format = macro->format; | |
930 | while (*format != '\0') | |
931 | { | |
932 | if (*format != '%') | |
933 | { | |
934 | ++len; | |
935 | ++format; | |
936 | } | |
937 | else | |
938 | { | |
939 | arg = strtol (format + 1, &send, 10); | |
940 | know (send != format && arg >= 0 && arg < count); | |
941 | len += strlen (operands[arg]); | |
942 | format = send; | |
943 | } | |
944 | } | |
945 | ||
946 | /* Put the string together. */ | |
947 | complete = s = (char *) alloca (len + 1); | |
948 | format = macro->format; | |
949 | while (*format != '\0') | |
950 | { | |
951 | if (*format != '%') | |
952 | *s++ = *format++; | |
953 | else | |
954 | { | |
955 | arg = strtol (format + 1, &send, 10); | |
956 | strcpy (s, operands[arg]); | |
957 | s += strlen (s); | |
958 | format = send; | |
959 | } | |
960 | } | |
961 | *s = '\0'; | |
962 | ||
963 | /* Assemble the constructed instruction. */ | |
964 | md_assemble (complete); | |
965 | } | |
966 | \f | |
967 | /* Pseudo-op handling. */ | |
968 | ||
969 | /* The .byte pseudo-op. This is similar to the normal .byte | |
970 | pseudo-op, but it can also take a single ASCII string. */ | |
971 | ||
972 | static void | |
973 | ppc_byte (ignore) | |
974 | int ignore; | |
975 | { | |
976 | if (*input_line_pointer != '\"') | |
977 | { | |
978 | cons (1); | |
979 | return; | |
980 | } | |
981 | ||
982 | /* Gather characters. A real double quote is doubled. Unusual | |
983 | characters are not permitted. */ | |
984 | ++input_line_pointer; | |
985 | while (1) | |
986 | { | |
987 | char c; | |
988 | ||
989 | c = *input_line_pointer++; | |
990 | ||
991 | if (c == '\"') | |
992 | { | |
993 | if (*input_line_pointer != '\"') | |
994 | break; | |
995 | ++input_line_pointer; | |
996 | } | |
997 | ||
998 | FRAG_APPEND_1_CHAR (c); | |
999 | } | |
1000 | ||
1001 | demand_empty_rest_of_line (); | |
1002 | } | |
1003 | \f | |
1004 | #ifdef OBJ_COFF | |
1005 | ||
1006 | /* XCOFF specific pseudo-op handling. */ | |
1007 | ||
1008 | /* The .comm and .lcomm pseudo-ops for XCOFF. XCOFF puts common | |
1009 | symbols in the .bss segment as though they were local common | |
1010 | symbols, and uses a different smclas. */ | |
1011 | ||
1012 | static void | |
1013 | ppc_comm (lcomm) | |
1014 | int lcomm; | |
1015 | { | |
1016 | asection *current_seg = now_seg; | |
1017 | subsegT current_subseg = now_subseg; | |
1018 | char *name; | |
1019 | char endc; | |
1020 | char *end_name; | |
1021 | offsetT size; | |
1022 | offsetT align; | |
1023 | symbolS *lcomm_sym = NULL; | |
1024 | symbolS *sym; | |
1025 | char *pfrag; | |
1026 | ||
1027 | name = input_line_pointer; | |
1028 | endc = get_symbol_end (); | |
1029 | end_name = input_line_pointer; | |
1030 | *end_name = endc; | |
1031 | ||
1032 | if (*input_line_pointer != ',') | |
1033 | { | |
1034 | as_bad ("missing size"); | |
1035 | ignore_rest_of_line (); | |
1036 | return; | |
1037 | } | |
1038 | ++input_line_pointer; | |
1039 | ||
1040 | size = get_absolute_expression (); | |
1041 | if (size < 0) | |
1042 | { | |
1043 | as_bad ("negative size"); | |
1044 | ignore_rest_of_line (); | |
1045 | return; | |
1046 | } | |
1047 | ||
1048 | if (! lcomm) | |
1049 | { | |
1050 | /* The third argument to .comm is the alignment. */ | |
1051 | if (*input_line_pointer != ',') | |
1052 | align = 3; | |
1053 | else | |
1054 | { | |
1055 | ++input_line_pointer; | |
1056 | align = get_absolute_expression (); | |
1057 | if (align <= 0) | |
1058 | { | |
1059 | as_warn ("ignoring bad alignment"); | |
1060 | align = 3; | |
1061 | } | |
1062 | } | |
1063 | } | |
1064 | else | |
1065 | { | |
1066 | char *lcomm_name; | |
1067 | char lcomm_endc; | |
1068 | ||
1069 | if (size <= 1) | |
1070 | align = 0; | |
1071 | else if (size <= 2) | |
1072 | align = 1; | |
1073 | else if (size <= 4) | |
1074 | align = 2; | |
1075 | else | |
1076 | align = 3; | |
1077 | ||
1078 | /* The third argument to .lcomm appears to be the real local | |
1079 | common symbol to create. References to the symbol named in | |
1080 | the first argument are turned into references to the third | |
1081 | argument. */ | |
1082 | if (*input_line_pointer != ',') | |
1083 | { | |
1084 | as_bad ("missing real symbol name"); | |
1085 | ignore_rest_of_line (); | |
1086 | return; | |
1087 | } | |
1088 | ++input_line_pointer; | |
1089 | ||
1090 | lcomm_name = input_line_pointer; | |
1091 | lcomm_endc = get_symbol_end (); | |
1092 | ||
1093 | lcomm_sym = symbol_find_or_make (lcomm_name); | |
1094 | ||
1095 | *input_line_pointer = lcomm_endc; | |
1096 | } | |
1097 | ||
1098 | *end_name = '\0'; | |
1099 | sym = symbol_find_or_make (name); | |
1100 | *end_name = endc; | |
1101 | ||
1102 | if (S_IS_DEFINED (sym) | |
1103 | || S_GET_VALUE (sym) != 0) | |
1104 | { | |
1105 | as_bad ("attempt to redefine symbol"); | |
1106 | ignore_rest_of_line (); | |
1107 | return; | |
1108 | } | |
1109 | ||
1110 | record_alignment (bss_section, align); | |
1111 | ||
1112 | if (! lcomm | |
1113 | || ! S_IS_DEFINED (lcomm_sym)) | |
1114 | { | |
1115 | symbolS *def_sym; | |
1116 | offsetT def_size; | |
1117 | ||
1118 | if (! lcomm) | |
1119 | { | |
1120 | def_sym = sym; | |
1121 | def_size = size; | |
1122 | S_SET_EXTERNAL (sym); | |
1123 | } | |
1124 | else | |
1125 | { | |
1126 | lcomm_sym->sy_tc.output = 1; | |
1127 | def_sym = lcomm_sym; | |
1128 | def_size = 0; | |
1129 | } | |
1130 | ||
1131 | subseg_set (bss_section, 1); | |
1132 | frag_align (align, 0); | |
1133 | ||
1134 | def_sym->sy_frag = frag_now; | |
1135 | pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, def_sym, | |
1136 | def_size, (char *) NULL); | |
1137 | *pfrag = 0; | |
1138 | S_SET_SEGMENT (def_sym, bss_section); | |
1139 | def_sym->sy_tc.align = align; | |
1140 | } | |
1141 | else if (lcomm) | |
1142 | { | |
1143 | /* Align the size of lcomm_sym. */ | |
1144 | lcomm_sym->sy_frag->fr_offset = | |
1145 | ((lcomm_sym->sy_frag->fr_offset + (1 << align) - 1) | |
1146 | &~ ((1 << align) - 1)); | |
1147 | if (align > lcomm_sym->sy_tc.align) | |
1148 | lcomm_sym->sy_tc.align = align; | |
1149 | } | |
1150 | ||
1151 | if (lcomm) | |
1152 | { | |
1153 | /* Make sym an offset from lcomm_sym. */ | |
1154 | S_SET_SEGMENT (sym, bss_section); | |
1155 | sym->sy_frag = lcomm_sym->sy_frag; | |
1156 | S_SET_VALUE (sym, lcomm_sym->sy_frag->fr_offset); | |
1157 | lcomm_sym->sy_frag->fr_offset += size; | |
1158 | } | |
1159 | ||
1160 | subseg_set (current_seg, current_subseg); | |
1161 | ||
1162 | demand_empty_rest_of_line (); | |
1163 | } | |
1164 | ||
1165 | /* The .csect pseudo-op. This switches us into a different | |
1166 | subsegment. The first argument is a symbol whose value is the | |
1167 | start of the .csect. In COFF, csect symbols get special aux | |
1168 | entries defined by the x_csect field of union internal_auxent. The | |
1169 | optional second argument is the alignment (the default is 2). */ | |
1170 | ||
1171 | static void | |
1172 | ppc_csect (ignore) | |
1173 | int ignore; | |
1174 | { | |
1175 | char *name; | |
1176 | char endc; | |
1177 | symbolS *sym; | |
1178 | ||
1179 | name = input_line_pointer; | |
1180 | endc = get_symbol_end (); | |
1181 | ||
1182 | sym = symbol_find_or_make (name); | |
1183 | ||
1184 | *input_line_pointer = endc; | |
1185 | ||
1186 | if (S_IS_DEFINED (sym)) | |
1187 | subseg_set (S_GET_SEGMENT (sym), sym->sy_tc.subseg); | |
1188 | else | |
1189 | { | |
1190 | symbolS **list_ptr; | |
1191 | int after_toc; | |
1192 | symbolS *list; | |
1193 | ||
1194 | /* This is a new csect. We need to look at the symbol class to | |
1195 | figure out whether it should go in the text section or the | |
1196 | data section. */ | |
1197 | after_toc = 0; | |
1198 | switch (sym->sy_tc.class) | |
1199 | { | |
1200 | case XMC_PR: | |
1201 | case XMC_RO: | |
1202 | case XMC_DB: | |
1203 | case XMC_GL: | |
1204 | case XMC_XO: | |
1205 | case XMC_SV: | |
1206 | case XMC_TI: | |
1207 | case XMC_TB: | |
1208 | S_SET_SEGMENT (sym, text_section); | |
1209 | sym->sy_tc.subseg = ppc_text_subsegment; | |
1210 | ++ppc_text_subsegment; | |
1211 | list_ptr = &ppc_text_csects; | |
1212 | break; | |
1213 | case XMC_RW: | |
1214 | case XMC_TC0: | |
1215 | case XMC_TC: | |
1216 | case XMC_DS: | |
1217 | case XMC_UA: | |
1218 | case XMC_BS: | |
1219 | case XMC_UC: | |
1220 | if (ppc_toc_csect->sy_tc.subseg + 1 == ppc_data_subsegment) | |
1221 | after_toc = 1; | |
1222 | S_SET_SEGMENT (sym, data_section); | |
1223 | sym->sy_tc.subseg = ppc_data_subsegment; | |
1224 | ++ppc_data_subsegment; | |
1225 | list_ptr = &ppc_data_csects; | |
1226 | break; | |
1227 | default: | |
1228 | abort (); | |
1229 | } | |
1230 | ||
1231 | subseg_new (segment_name (S_GET_SEGMENT (sym)), sym->sy_tc.subseg); | |
1232 | if (after_toc) | |
1233 | ppc_after_toc_frag = frag_now; | |
1234 | ||
1235 | sym->sy_frag = frag_now; | |
1236 | S_SET_VALUE (sym, (valueT) frag_now_fix ()); | |
1237 | ||
1238 | sym->sy_tc.align = 2; | |
1239 | sym->sy_tc.output = 1; | |
1240 | sym->sy_tc.within = sym; | |
1241 | ||
1242 | for (list = *list_ptr; | |
1243 | list->sy_tc.next != (symbolS *) NULL; | |
1244 | list = list->sy_tc.next) | |
1245 | ; | |
1246 | list->sy_tc.next = sym; | |
1247 | ||
1248 | symbol_remove (sym, &symbol_rootP, &symbol_lastP); | |
1249 | symbol_append (sym, list->sy_tc.within, &symbol_rootP, &symbol_lastP); | |
1250 | } | |
1251 | ||
1252 | if (*input_line_pointer == ',') | |
1253 | { | |
1254 | ++input_line_pointer; | |
1255 | sym->sy_tc.align = get_absolute_expression (); | |
1256 | } | |
1257 | ||
1258 | ppc_current_csect = sym; | |
1259 | ||
1260 | demand_empty_rest_of_line (); | |
1261 | } | |
1262 | ||
1263 | /* The .extern pseudo-op. We create an undefined symbol. */ | |
1264 | ||
1265 | static void | |
1266 | ppc_extern (ignore) | |
1267 | int ignore; | |
1268 | { | |
1269 | char *name; | |
1270 | char endc; | |
1271 | ||
1272 | name = input_line_pointer; | |
1273 | endc = get_symbol_end (); | |
1274 | ||
1275 | (void) symbol_find_or_make (name); | |
1276 | ||
1277 | *input_line_pointer = endc; | |
1278 | ||
1279 | demand_empty_rest_of_line (); | |
1280 | } | |
1281 | ||
1282 | /* The .lglobl pseudo-op. I think the RS/6000 assembler only needs | |
1283 | this because it can't handle undefined symbols. I think we can | |
1284 | just ignore it. */ | |
1285 | ||
1286 | static void | |
1287 | ppc_lglobl (ignore) | |
1288 | int ignore; | |
1289 | { | |
1290 | s_ignore (0); | |
1291 | } | |
1292 | ||
1293 | /* The .rename pseudo-op. The RS/6000 assembler can rename symbols, | |
1294 | although I don't know why it bothers. */ | |
1295 | ||
1296 | static void | |
1297 | ppc_rename (ignore) | |
1298 | int ignore; | |
1299 | { | |
1300 | char *name; | |
1301 | char endc; | |
1302 | symbolS *sym; | |
1303 | int len; | |
1304 | ||
1305 | name = input_line_pointer; | |
1306 | endc = get_symbol_end (); | |
1307 | ||
1308 | sym = symbol_find_or_make (name); | |
1309 | ||
1310 | *input_line_pointer = endc; | |
1311 | ||
1312 | if (*input_line_pointer != ',') | |
1313 | { | |
1314 | as_bad ("missing rename string"); | |
1315 | ignore_rest_of_line (); | |
1316 | return; | |
1317 | } | |
1318 | ++input_line_pointer; | |
1319 | ||
1320 | sym->sy_tc.real_name = demand_copy_C_string (&len); | |
1321 | ||
1322 | demand_empty_rest_of_line (); | |
1323 | } | |
1324 | ||
1325 | /* The .stabx pseudo-op. This is similar to a normal .stabs | |
1326 | pseudo-op, but slightly different. A sample is | |
1327 | .stabx "main:F-1",.main,142,0 | |
1328 | The first argument is the symbol name to create. The second is the | |
1329 | value, and the third is the storage class. The fourth seems to be | |
1330 | always zero, and I am assuming it is the type. */ | |
1331 | ||
1332 | static void | |
1333 | ppc_stabx (ignore) | |
1334 | int ignore; | |
1335 | { | |
1336 | char *name; | |
1337 | int len; | |
1338 | symbolS *sym; | |
1eeb357e | 1339 | expressionS exp; |
882bdc69 ILT |
1340 | |
1341 | name = demand_copy_C_string (&len); | |
1342 | ||
1343 | if (*input_line_pointer != ',') | |
1344 | { | |
1345 | as_bad ("missing value"); | |
1346 | return; | |
1347 | } | |
1348 | ++input_line_pointer; | |
1349 | ||
1350 | sym = symbol_make (name); | |
1eeb357e ILT |
1351 | |
1352 | (void) expression (&exp); | |
1353 | ||
1354 | switch (exp.X_op) | |
1355 | { | |
1356 | case O_illegal: | |
1357 | case O_absent: | |
1358 | case O_big: | |
1359 | as_bad ("illegal .stabx expression; zero assumed"); | |
1360 | exp.X_add_number = 0; | |
1361 | /* Fall through. */ | |
1362 | case O_constant: | |
1363 | S_SET_VALUE (sym, (valueT) exp.X_add_number); | |
1364 | sym->sy_frag = &zero_address_frag; | |
1365 | break; | |
1366 | ||
1367 | case O_symbol: | |
1368 | if (S_GET_SEGMENT (exp.X_add_symbol) == undefined_section) | |
1369 | sym->sy_value = exp; | |
1370 | else | |
1371 | { | |
1372 | S_SET_VALUE (sym, | |
1373 | exp.X_add_number + S_GET_VALUE (exp.X_add_symbol)); | |
1374 | sym->sy_frag = exp.X_add_symbol->sy_frag; | |
1375 | } | |
1376 | break; | |
1377 | ||
1378 | default: | |
1379 | /* The value is some complex expression. This will probably | |
1380 | fail at some later point, but this is probably the right | |
1381 | thing to do here. */ | |
1382 | sym->sy_value = exp; | |
1383 | break; | |
1384 | } | |
882bdc69 ILT |
1385 | |
1386 | S_SET_SEGMENT (sym, ppc_coff_debug_section); | |
1387 | sym->bsym->flags |= BSF_DEBUGGING; | |
1388 | ||
1389 | if (*input_line_pointer != ',') | |
1390 | { | |
1391 | as_bad ("missing class"); | |
1392 | return; | |
1393 | } | |
1394 | ++input_line_pointer; | |
1395 | ||
1396 | S_SET_STORAGE_CLASS (sym, get_absolute_expression ()); | |
1397 | ||
1398 | if (*input_line_pointer != ',') | |
1399 | { | |
1400 | as_bad ("missing type"); | |
1401 | return; | |
1402 | } | |
1403 | ++input_line_pointer; | |
1404 | ||
1405 | S_SET_DATA_TYPE (sym, get_absolute_expression ()); | |
1406 | ||
1407 | sym->sy_tc.output = 1; | |
1eeb357e ILT |
1408 | |
1409 | if (S_GET_STORAGE_CLASS (sym) == C_STSYM) | |
1410 | sym->sy_tc.within = ppc_current_block; | |
1411 | ||
1412 | if (exp.X_op != O_symbol | |
1413 | || ! S_IS_EXTERNAL (exp.X_add_symbol) | |
1414 | || S_GET_SEGMENT (exp.X_add_symbol) != bss_section) | |
1415 | ppc_frob_label (sym); | |
1416 | else | |
1417 | { | |
1418 | symbol_remove (sym, &symbol_rootP, &symbol_lastP); | |
1419 | symbol_append (sym, exp.X_add_symbol, &symbol_rootP, &symbol_lastP); | |
1420 | if (ppc_current_csect->sy_tc.within == exp.X_add_symbol) | |
1421 | ppc_current_csect->sy_tc.within = sym; | |
1422 | } | |
882bdc69 ILT |
1423 | |
1424 | if (strlen (name) > SYMNMLEN) | |
1425 | { | |
1426 | /* For some reason, each name is preceded by a two byte length | |
1427 | and followed by a null byte. */ | |
1428 | ppc_debug_name_section_size += strlen (name) + 3; | |
1429 | } | |
1430 | ||
1431 | demand_empty_rest_of_line (); | |
1432 | } | |
1433 | ||
1434 | /* The .function pseudo-op. This takes several arguments. The first | |
1435 | argument seems to be the external name of the symbol. The second | |
1436 | argment seems to be the label for the start of the function. gcc | |
1437 | uses the same name for both. I have no idea what the third and | |
1438 | fourth arguments are meant to be. The optional fifth argument is | |
1439 | an expression for the size of the function. In COFF this symbol | |
1440 | gets an aux entry like that used for a csect. */ | |
1441 | ||
1442 | static void | |
1443 | ppc_function (ignore) | |
1444 | int ignore; | |
1445 | { | |
1446 | char *name; | |
1447 | char endc; | |
1448 | char *s; | |
1449 | symbolS *ext_sym; | |
1450 | symbolS *lab_sym; | |
1451 | ||
1452 | name = input_line_pointer; | |
1453 | endc = get_symbol_end (); | |
1454 | ||
1455 | /* Ignore any [PR] suffix. */ | |
1456 | name = ppc_canonicalize_symbol_name (name); | |
1457 | s = strchr (name, '['); | |
1458 | if (s != (char *) NULL | |
1459 | && strcmp (s + 1, "PR]") == 0) | |
1460 | *s = '\0'; | |
1461 | ||
1462 | ext_sym = symbol_find_or_make (name); | |
1463 | ||
1464 | *input_line_pointer = endc; | |
1465 | ||
1466 | if (*input_line_pointer != ',') | |
1467 | { | |
1468 | as_bad ("missing symbol name"); | |
1469 | ignore_rest_of_line (); | |
1470 | return; | |
1471 | } | |
1472 | ++input_line_pointer; | |
1473 | ||
1474 | name = input_line_pointer; | |
1475 | endc = get_symbol_end (); | |
1476 | ||
1477 | lab_sym = symbol_find_or_make (name); | |
1478 | ||
1479 | *input_line_pointer = endc; | |
1480 | ||
1481 | if (ext_sym != lab_sym) | |
1482 | { | |
1483 | ext_sym->sy_value.X_op = O_symbol; | |
1484 | ext_sym->sy_value.X_add_symbol = lab_sym; | |
1485 | ext_sym->sy_value.X_op_symbol = NULL; | |
1486 | ext_sym->sy_value.X_add_number = 0; | |
1487 | } | |
1488 | ||
1489 | if (ext_sym->sy_tc.class == -1) | |
1490 | ext_sym->sy_tc.class = XMC_PR; | |
1491 | ext_sym->sy_tc.output = 1; | |
1492 | ||
1493 | if (*input_line_pointer == ',') | |
1494 | { | |
1495 | expressionS ignore; | |
1496 | ||
1497 | /* Ignore the third argument. */ | |
1498 | ++input_line_pointer; | |
1499 | expression (&ignore); | |
1500 | if (*input_line_pointer == ',') | |
1501 | { | |
1502 | /* Ignore the fourth argument. */ | |
1503 | ++input_line_pointer; | |
1504 | expression (&ignore); | |
1505 | if (*input_line_pointer == ',') | |
1506 | { | |
1507 | /* The fifth argument is the function size. */ | |
1508 | ++input_line_pointer; | |
1509 | ext_sym->sy_tc.size = symbol_new ("L0\001", | |
1510 | absolute_section, | |
1511 | (valueT) 0, | |
1512 | &zero_address_frag); | |
1513 | pseudo_set (ext_sym->sy_tc.size); | |
1514 | } | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | S_SET_DATA_TYPE (ext_sym, DT_FCN << N_BTSHFT); | |
1519 | SF_SET_FUNCTION (ext_sym); | |
1520 | SF_SET_PROCESS (ext_sym); | |
1521 | coff_add_linesym (ext_sym); | |
1522 | ||
1523 | demand_empty_rest_of_line (); | |
1524 | } | |
1525 | ||
1526 | /* The .bf pseudo-op. This is just like a COFF C_FCN symbol named | |
1527 | ".bf". */ | |
1528 | ||
1529 | static void | |
1530 | ppc_bf (ignore) | |
1531 | int ignore; | |
1532 | { | |
1533 | symbolS *sym; | |
882bdc69 ILT |
1534 | |
1535 | sym = symbol_make (".bf"); | |
1536 | S_SET_SEGMENT (sym, text_section); | |
1537 | sym->sy_frag = frag_now; | |
1538 | S_SET_VALUE (sym, frag_now_fix ()); | |
1539 | S_SET_STORAGE_CLASS (sym, C_FCN); | |
1540 | ||
1eeb357e | 1541 | coff_line_base = get_absolute_expression (); |
882bdc69 ILT |
1542 | |
1543 | S_SET_NUMBER_AUXILIARY (sym, 1); | |
1544 | SA_SET_SYM_LNNO (sym, coff_line_base); | |
1545 | ||
1546 | sym->sy_tc.output = 1; | |
1547 | ||
1548 | ppc_frob_label (sym); | |
1549 | ||
1550 | demand_empty_rest_of_line (); | |
1551 | } | |
1552 | ||
1553 | /* The .ef pseudo-op. This is just like a COFF C_FCN symbol named | |
1554 | ".ef", except that the line number is absolute, not relative to the | |
1555 | most recent ".bf" symbol. */ | |
1556 | ||
1557 | static void | |
1558 | ppc_ef (ignore) | |
1559 | int ignore; | |
1560 | { | |
1561 | symbolS *sym; | |
1562 | ||
1563 | sym = symbol_make (".ef"); | |
1564 | S_SET_SEGMENT (sym, text_section); | |
1565 | sym->sy_frag = frag_now; | |
1566 | S_SET_VALUE (sym, frag_now_fix ()); | |
1567 | S_SET_STORAGE_CLASS (sym, C_FCN); | |
1568 | S_SET_NUMBER_AUXILIARY (sym, 1); | |
1569 | SA_SET_SYM_LNNO (sym, get_absolute_expression ()); | |
1570 | sym->sy_tc.output = 1; | |
1571 | ||
1572 | ppc_frob_label (sym); | |
1573 | ||
1574 | demand_empty_rest_of_line (); | |
1575 | } | |
1576 | ||
1577 | /* The .bi and .ei pseudo-ops. These take a string argument and | |
1578 | generates a C_BINCL or C_EINCL symbol, which goes at the start of | |
1579 | the symbol list. */ | |
1580 | ||
1581 | static void | |
1582 | ppc_biei (ei) | |
1583 | int ei; | |
1584 | { | |
1585 | char *name; | |
1586 | int len; | |
1587 | symbolS *sym; | |
1588 | symbolS *look; | |
1589 | ||
1590 | name = demand_copy_C_string (&len); | |
1591 | ||
1592 | sym = symbol_make (name); | |
1593 | S_SET_SEGMENT (sym, ppc_coff_debug_section); | |
1594 | sym->bsym->flags |= BSF_DEBUGGING; | |
1595 | ||
1596 | /* FIXME: The value of the .bi or .ei symbol is supposed to be the | |
1597 | offset in the file to the line number entry to use. That is | |
1598 | quite difficult to implement using BFD, so I'm just not doing it. | |
1599 | Sorry. Please add it if you can figure out how. Note that this | |
1600 | approach is the only way to support multiple files in COFF, since | |
1601 | line numbers are associated with function symbols. Note further | |
1602 | that it still doesn't work, since the line numbers are stored as | |
1603 | offsets from a base line number. */ | |
1604 | ||
1605 | S_SET_STORAGE_CLASS (sym, ei ? C_EINCL : C_BINCL); | |
1606 | sym->sy_tc.output = 1; | |
1607 | ||
1608 | for (look = symbol_rootP; | |
1609 | (look != (symbolS *) NULL | |
1610 | && (S_GET_STORAGE_CLASS (look) == C_FILE | |
1611 | || S_GET_STORAGE_CLASS (look) == C_BINCL | |
1612 | || S_GET_STORAGE_CLASS (look) == C_EINCL)); | |
1613 | look = symbol_next (look)) | |
1614 | ; | |
1615 | if (look != (symbolS *) NULL) | |
1616 | { | |
1617 | symbol_remove (sym, &symbol_rootP, &symbol_lastP); | |
1618 | symbol_insert (sym, look, &symbol_rootP, &symbol_lastP); | |
1619 | } | |
1620 | ||
1621 | demand_empty_rest_of_line (); | |
1622 | } | |
1623 | ||
1624 | /* The .bs pseudo-op. This generates a C_BSTAT symbol named ".bs". | |
1625 | There is one argument, which is a csect symbol. The value of the | |
1626 | .bs symbol is the index of this csect symbol. */ | |
1627 | ||
1628 | static void | |
1629 | ppc_bs (ignore) | |
1630 | int ignore; | |
1631 | { | |
1632 | char *name; | |
1633 | char endc; | |
1634 | symbolS *csect; | |
1635 | symbolS *sym; | |
1636 | ||
1eeb357e ILT |
1637 | if (ppc_current_block != NULL) |
1638 | as_bad ("nested .bs blocks"); | |
1639 | ||
882bdc69 ILT |
1640 | name = input_line_pointer; |
1641 | endc = get_symbol_end (); | |
1642 | ||
1643 | csect = symbol_find_or_make (name); | |
1644 | ||
1645 | *input_line_pointer = endc; | |
1646 | ||
1647 | sym = symbol_make (".bs"); | |
1648 | S_SET_SEGMENT (sym, now_seg); | |
1649 | S_SET_STORAGE_CLASS (sym, C_BSTAT); | |
1650 | sym->bsym->flags |= BSF_DEBUGGING; | |
1651 | sym->sy_tc.output = 1; | |
1652 | ||
1653 | sym->sy_tc.within = csect; | |
1654 | ||
1655 | ppc_frob_label (sym); | |
1656 | ||
1eeb357e ILT |
1657 | ppc_current_block = sym; |
1658 | ||
882bdc69 ILT |
1659 | demand_empty_rest_of_line (); |
1660 | } | |
1661 | ||
1662 | /* The .es pseudo-op. Generate a C_ESTART symbol named .es. */ | |
1663 | ||
1664 | static void | |
1665 | ppc_es (ignore) | |
1666 | int ignore; | |
1667 | { | |
1668 | symbolS *sym; | |
1669 | ||
1eeb357e ILT |
1670 | if (ppc_current_block == NULL) |
1671 | as_bad (".es without preceding .bs"); | |
1672 | ||
882bdc69 ILT |
1673 | sym = symbol_make (".es"); |
1674 | S_SET_SEGMENT (sym, now_seg); | |
1675 | S_SET_STORAGE_CLASS (sym, C_ESTAT); | |
1676 | sym->bsym->flags |= BSF_DEBUGGING; | |
1677 | sym->sy_tc.output = 1; | |
1678 | ||
1679 | ppc_frob_label (sym); | |
1680 | ||
1eeb357e ILT |
1681 | ppc_current_block = NULL; |
1682 | ||
882bdc69 ILT |
1683 | demand_empty_rest_of_line (); |
1684 | } | |
1685 | ||
1686 | /* The .bb pseudo-op. Generate a C_BLOCK symbol named .bb, with a | |
1687 | line number. */ | |
1688 | ||
1689 | static void | |
1690 | ppc_bb (ignore) | |
1691 | int ignore; | |
1692 | { | |
1693 | symbolS *sym; | |
1694 | ||
1695 | sym = symbol_make (".bb"); | |
1696 | S_SET_SEGMENT (sym, text_section); | |
1697 | sym->sy_frag = frag_now; | |
1698 | S_SET_VALUE (sym, frag_now_fix ()); | |
1699 | S_SET_STORAGE_CLASS (sym, C_BLOCK); | |
1700 | ||
1701 | S_SET_NUMBER_AUXILIARY (sym, 1); | |
1702 | SA_SET_SYM_LNNO (sym, get_absolute_expression ()); | |
1703 | ||
1704 | sym->sy_tc.output = 1; | |
1705 | ||
1706 | ppc_frob_label (sym); | |
1707 | ||
1708 | demand_empty_rest_of_line (); | |
1709 | } | |
1710 | ||
1711 | /* The .eb pseudo-op. Generate a C_BLOCK symbol named .eb, with a | |
1712 | line number. */ | |
1713 | ||
1714 | static void | |
1715 | ppc_eb (ignore) | |
1716 | int ignore; | |
1717 | { | |
1718 | symbolS *sym; | |
1719 | ||
1720 | sym = symbol_make (".eb"); | |
1721 | S_SET_SEGMENT (sym, text_section); | |
1722 | sym->sy_frag = frag_now; | |
1723 | S_SET_VALUE (sym, frag_now_fix ()); | |
1724 | S_SET_STORAGE_CLASS (sym, C_FCN); | |
1725 | S_SET_NUMBER_AUXILIARY (sym, 1); | |
1726 | SA_SET_SYM_LNNO (sym, get_absolute_expression ()); | |
1727 | sym->sy_tc.output = 1; | |
1728 | ||
1729 | ppc_frob_label (sym); | |
1730 | ||
1731 | demand_empty_rest_of_line (); | |
1732 | } | |
1733 | ||
1734 | /* The .toc pseudo-op. Switch to the .toc subsegment. */ | |
1735 | ||
1736 | static void | |
1737 | ppc_toc (ignore) | |
1738 | int ignore; | |
1739 | { | |
1740 | if (ppc_toc_csect != (symbolS *) NULL) | |
1741 | subseg_set (data_section, ppc_toc_csect->sy_tc.subseg); | |
1742 | else | |
1743 | { | |
1744 | subsegT subseg; | |
1745 | symbolS *sym; | |
1746 | symbolS *list; | |
1747 | ||
1748 | subseg = ppc_data_subsegment; | |
1749 | ++ppc_data_subsegment; | |
1750 | ||
1751 | subseg_new (segment_name (data_section), subseg); | |
1752 | ppc_toc_frag = frag_now; | |
1753 | ||
1754 | sym = symbol_find_or_make ("TOC[TC0]"); | |
1755 | sym->sy_frag = frag_now; | |
1756 | S_SET_SEGMENT (sym, data_section); | |
1757 | S_SET_VALUE (sym, (valueT) frag_now_fix ()); | |
1758 | sym->sy_tc.subseg = subseg; | |
1759 | sym->sy_tc.output = 1; | |
1760 | sym->sy_tc.within = sym; | |
1761 | ||
1762 | ppc_toc_csect = sym; | |
1763 | ||
1764 | for (list = ppc_data_csects; | |
1765 | list->sy_tc.next != (symbolS *) NULL; | |
1766 | list = list->sy_tc.next) | |
1767 | ; | |
1768 | list->sy_tc.next = sym; | |
1769 | ||
1770 | symbol_remove (sym, &symbol_rootP, &symbol_lastP); | |
1771 | symbol_append (sym, list->sy_tc.within, &symbol_rootP, &symbol_lastP); | |
1772 | } | |
1773 | ||
1774 | ppc_current_csect = ppc_toc_csect; | |
1775 | ||
1776 | demand_empty_rest_of_line (); | |
1777 | } | |
1778 | ||
1779 | #endif /* OBJ_COFF */ | |
1780 | \f | |
1781 | /* The .tc pseudo-op. This is used when generating either XCOFF or | |
1782 | ELF. This takes two or more arguments. | |
1783 | ||
1784 | When generating XCOFF output, the first argument is the name to | |
1785 | give to this location in the toc; this will be a symbol with class | |
1786 | TC. The rest of the arguments are 4 byte values to actually put at | |
1787 | this location in the TOC; often there is just one more argument, a | |
1788 | relocateable symbol reference. | |
1789 | ||
1790 | When not generating XCOFF output, the arguments are the same, but | |
1791 | the first argument is simply ignored. */ | |
1792 | ||
1793 | static void | |
1794 | ppc_tc (ignore) | |
1795 | int ignore; | |
1796 | { | |
1797 | #ifdef OBJ_COFF | |
1798 | ||
1799 | /* Define the TOC symbol name. */ | |
1800 | { | |
1801 | char *name; | |
1802 | char endc; | |
1803 | symbolS *sym; | |
1804 | ||
1805 | if (ppc_toc_csect == (symbolS *) NULL | |
1806 | || ppc_toc_csect != ppc_current_csect) | |
1807 | { | |
1808 | as_bad (".tc not in .toc section"); | |
1809 | ignore_rest_of_line (); | |
1810 | return; | |
1811 | } | |
1812 | ||
1813 | name = input_line_pointer; | |
1814 | endc = get_symbol_end (); | |
1815 | ||
1816 | sym = symbol_find_or_make (name); | |
1817 | ||
1818 | *input_line_pointer = endc; | |
1819 | ||
1820 | if (S_IS_DEFINED (sym)) | |
1821 | { | |
1822 | symbolS *label; | |
1823 | ||
1824 | label = ppc_current_csect->sy_tc.within; | |
1825 | if (label->sy_tc.class != XMC_TC0) | |
1826 | { | |
1827 | as_warn (".tc with no label"); | |
1828 | ignore_rest_of_line (); | |
1829 | return; | |
1830 | } | |
1831 | ||
1832 | S_SET_SEGMENT (label, S_GET_SEGMENT (sym)); | |
1833 | label->sy_frag = sym->sy_frag; | |
1834 | S_SET_VALUE (label, S_GET_VALUE (sym)); | |
1835 | ||
1836 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
1837 | ++input_line_pointer; | |
1838 | ||
1839 | return; | |
1840 | } | |
1841 | ||
1842 | S_SET_SEGMENT (sym, now_seg); | |
1843 | sym->sy_frag = frag_now; | |
1844 | S_SET_VALUE (sym, (valueT) frag_now_fix ()); | |
1845 | sym->sy_tc.class = XMC_TC; | |
1846 | sym->sy_tc.output = 1; | |
1847 | ||
1848 | ppc_frob_label (sym); | |
1849 | } | |
1850 | ||
1851 | #else /* ! defined (OBJ_COFF) */ | |
1852 | ||
1853 | /* Skip the TOC symbol name. */ | |
1854 | while (is_part_of_name (*input_line_pointer) | |
1855 | || *input_line_pointer == '[' | |
1856 | || *input_line_pointer == ']' | |
1857 | || *input_line_pointer == '{' | |
1858 | || *input_line_pointer == '}') | |
1859 | ++input_line_pointer; | |
1860 | ||
1eeb357e ILT |
1861 | /* Align to a four byte boundary. */ |
1862 | frag_align (2, 0); | |
1863 | record_alignment (now_seg, 2); | |
1864 | ||
882bdc69 ILT |
1865 | #endif /* ! defined (OBJ_COFF) */ |
1866 | ||
1867 | if (*input_line_pointer != ',') | |
1868 | demand_empty_rest_of_line (); | |
1869 | else | |
1870 | { | |
1871 | ++input_line_pointer; | |
1872 | cons (4); | |
1873 | } | |
1874 | } | |
1875 | \f | |
1876 | #ifdef OBJ_COFF | |
1877 | ||
1878 | /* XCOFF specific symbol and file handling. */ | |
1879 | ||
1880 | /* Canonicalize the symbol name. We use the to force the suffix, if | |
1881 | any, to use square brackets, and to be in upper case. */ | |
1882 | ||
1883 | char * | |
1884 | ppc_canonicalize_symbol_name (name) | |
1885 | char *name; | |
1886 | { | |
1887 | char *s; | |
1888 | ||
1889 | for (s = name; *s != '\0' && *s != '{' && *s != '['; s++) | |
1890 | ; | |
1891 | if (*s != '\0') | |
1892 | { | |
1893 | char brac; | |
1894 | ||
1895 | if (*s == '[') | |
1896 | brac = ']'; | |
1897 | else | |
1898 | { | |
1899 | *s = '['; | |
1900 | brac = '}'; | |
1901 | } | |
1902 | ||
1903 | for (s++; *s != '\0' && *s != brac; s++) | |
1904 | if (islower (*s)) | |
1905 | *s = toupper (*s); | |
1906 | ||
1907 | if (*s == '\0' || s[1] != '\0') | |
1908 | as_bad ("bad symbol suffix"); | |
1909 | ||
1910 | *s = ']'; | |
1911 | } | |
1912 | ||
1913 | return name; | |
1914 | } | |
1915 | ||
1916 | /* Set the class of a symbol based on the suffix, if any. This is | |
1917 | called whenever a new symbol is created. */ | |
1918 | ||
1919 | void | |
1920 | ppc_symbol_new_hook (sym) | |
1921 | symbolS *sym; | |
1922 | { | |
1923 | const char *s; | |
1924 | ||
1925 | sym->sy_tc.next = NULL; | |
1926 | sym->sy_tc.output = 0; | |
1927 | sym->sy_tc.class = -1; | |
1928 | sym->sy_tc.real_name = NULL; | |
1929 | sym->sy_tc.subseg = 0; | |
1930 | sym->sy_tc.align = 0; | |
1931 | sym->sy_tc.size = NULL; | |
1932 | sym->sy_tc.within = NULL; | |
1933 | ||
1934 | s = strchr (S_GET_NAME (sym), '['); | |
1935 | if (s == (const char *) NULL) | |
1936 | { | |
1937 | /* There is no suffix. */ | |
1938 | return; | |
1939 | } | |
1940 | ||
1941 | ++s; | |
1942 | ||
1943 | switch (s[0]) | |
1944 | { | |
1945 | case 'B': | |
1946 | if (strcmp (s, "BS]") == 0) | |
1947 | sym->sy_tc.class = XMC_BS; | |
1948 | break; | |
1949 | case 'D': | |
1950 | if (strcmp (s, "DB]") == 0) | |
1951 | sym->sy_tc.class = XMC_DB; | |
1952 | else if (strcmp (s, "DS]") == 0) | |
1953 | sym->sy_tc.class = XMC_DS; | |
1954 | break; | |
1955 | case 'G': | |
1956 | if (strcmp (s, "GL]") == 0) | |
1957 | sym->sy_tc.class = XMC_GL; | |
1958 | break; | |
1959 | case 'P': | |
1960 | if (strcmp (s, "PR]") == 0) | |
1961 | sym->sy_tc.class = XMC_PR; | |
1962 | break; | |
1963 | case 'R': | |
1964 | if (strcmp (s, "RO]") == 0) | |
1965 | sym->sy_tc.class = XMC_RO; | |
1966 | else if (strcmp (s, "RW]") == 0) | |
1967 | sym->sy_tc.class = XMC_RW; | |
1968 | break; | |
1969 | case 'S': | |
1970 | if (strcmp (s, "SV]") == 0) | |
1971 | sym->sy_tc.class = XMC_SV; | |
1972 | break; | |
1973 | case 'T': | |
1974 | if (strcmp (s, "TC]") == 0) | |
1975 | sym->sy_tc.class = XMC_TC; | |
1976 | else if (strcmp (s, "TI]") == 0) | |
1977 | sym->sy_tc.class = XMC_TI; | |
1978 | else if (strcmp (s, "TB]") == 0) | |
1979 | sym->sy_tc.class = XMC_TB; | |
4a6b2f8b | 1980 | else if (strcmp (s, "TC0]") == 0 || strcmp (s, "T0]") == 0) |
882bdc69 ILT |
1981 | sym->sy_tc.class = XMC_TC0; |
1982 | break; | |
1983 | case 'U': | |
1984 | if (strcmp (s, "UA]") == 0) | |
1985 | sym->sy_tc.class = XMC_UA; | |
1986 | else if (strcmp (s, "UC]") == 0) | |
1987 | sym->sy_tc.class = XMC_UC; | |
1988 | break; | |
1989 | case 'X': | |
1990 | if (strcmp (s, "XO]") == 0) | |
1991 | sym->sy_tc.class = XMC_XO; | |
1992 | break; | |
1993 | } | |
1994 | ||
1995 | if (sym->sy_tc.class == -1) | |
1996 | as_bad ("Unrecognized symbol suffix"); | |
1997 | } | |
1998 | ||
1999 | /* Set the class of a label based on where it is defined. This | |
2000 | handles symbols without suffixes. Also, move the symbol so that it | |
2001 | follows the csect symbol. */ | |
2002 | ||
2003 | void | |
2004 | ppc_frob_label (sym) | |
2005 | symbolS *sym; | |
2006 | { | |
2007 | if (ppc_current_csect != (symbolS *) NULL) | |
2008 | { | |
2009 | if (sym->sy_tc.class == -1) | |
2010 | sym->sy_tc.class = ppc_current_csect->sy_tc.class; | |
2011 | ||
2012 | symbol_remove (sym, &symbol_rootP, &symbol_lastP); | |
2013 | symbol_append (sym, ppc_current_csect->sy_tc.within, &symbol_rootP, | |
2014 | &symbol_lastP); | |
2015 | ppc_current_csect->sy_tc.within = sym; | |
2016 | } | |
2017 | } | |
2018 | ||
2019 | /* Change the name of a symbol just before writing it out. Set the | |
2020 | real name if the .rename pseudo-op was used. Otherwise, remove any | |
2021 | class suffix. Return 1 if the symbol should not be included in the | |
2022 | symbol table. */ | |
2023 | ||
2024 | int | |
2025 | ppc_frob_symbol (sym) | |
2026 | symbolS *sym; | |
2027 | { | |
2028 | static symbolS *ppc_last_function; | |
2029 | static symbolS *set_end; | |
2030 | ||
2031 | /* Discard symbols that should not be included in the output symbol | |
2032 | table. */ | |
2033 | if (! sym->sy_used_in_reloc | |
2034 | && ((sym->bsym->flags & BSF_SECTION_SYM) != 0 | |
2035 | || (! S_IS_EXTERNAL (sym) | |
2036 | && ! sym->sy_tc.output | |
2037 | && S_GET_STORAGE_CLASS (sym) != C_FILE))) | |
2038 | return 1; | |
2039 | ||
2040 | if (sym->sy_tc.real_name != (char *) NULL) | |
2041 | S_SET_NAME (sym, sym->sy_tc.real_name); | |
2042 | else | |
2043 | { | |
2044 | const char *name; | |
2045 | const char *s; | |
2046 | ||
2047 | name = S_GET_NAME (sym); | |
2048 | s = strchr (name, '['); | |
2049 | if (s != (char *) NULL) | |
2050 | { | |
2051 | unsigned int len; | |
2052 | char *snew; | |
2053 | ||
2054 | len = s - name; | |
2055 | snew = xmalloc (len + 1); | |
2056 | memcpy (snew, name, len); | |
2057 | snew[len] = '\0'; | |
2058 | ||
2059 | S_SET_NAME (sym, snew); | |
2060 | } | |
2061 | } | |
2062 | ||
2063 | if (set_end != (symbolS *) NULL) | |
2064 | { | |
2065 | SA_SET_SYM_ENDNDX (set_end, sym); | |
2066 | set_end = NULL; | |
2067 | } | |
2068 | ||
2069 | if (SF_GET_FUNCTION (sym)) | |
2070 | { | |
2071 | if (ppc_last_function != (symbolS *) NULL) | |
2072 | as_warn ("two .function pseudo-ops with no intervening .ef"); | |
2073 | ppc_last_function = sym; | |
2074 | if (sym->sy_tc.size != (symbolS *) NULL) | |
2075 | { | |
2076 | resolve_symbol_value (sym->sy_tc.size); | |
2077 | SA_SET_SYM_FSIZE (sym, (long) S_GET_VALUE (sym->sy_tc.size)); | |
2078 | } | |
2079 | } | |
2080 | else if (S_GET_STORAGE_CLASS (sym) == C_FCN | |
2081 | && strcmp (S_GET_NAME (sym), ".ef") == 0) | |
2082 | { | |
2083 | if (ppc_last_function == (symbolS *) NULL) | |
2084 | as_warn (".ef with no preceding .function"); | |
2085 | else | |
2086 | { | |
2087 | set_end = ppc_last_function; | |
2088 | ppc_last_function = NULL; | |
2089 | ||
2090 | /* We don't have a C_EFCN symbol, but we need to force the | |
2091 | COFF backend to believe that it has seen one. */ | |
2092 | coff_last_function = NULL; | |
2093 | } | |
2094 | } | |
2095 | ||
2096 | if (! S_IS_EXTERNAL (sym) | |
2097 | && (sym->bsym->flags & BSF_SECTION_SYM) == 0 | |
2098 | && S_GET_STORAGE_CLASS (sym) != C_FILE | |
2099 | && S_GET_STORAGE_CLASS (sym) != C_FCN | |
2100 | && S_GET_STORAGE_CLASS (sym) != C_BSTAT | |
2101 | && S_GET_STORAGE_CLASS (sym) != C_ESTAT | |
2102 | && S_GET_SEGMENT (sym) != ppc_coff_debug_section) | |
2103 | S_SET_STORAGE_CLASS (sym, C_HIDEXT); | |
2104 | ||
2105 | if (S_GET_STORAGE_CLASS (sym) == C_EXT | |
2106 | || S_GET_STORAGE_CLASS (sym) == C_HIDEXT) | |
2107 | { | |
2108 | int i; | |
2109 | union internal_auxent *a; | |
2110 | ||
2111 | /* Create a csect aux. */ | |
2112 | i = S_GET_NUMBER_AUXILIARY (sym); | |
2113 | S_SET_NUMBER_AUXILIARY (sym, i + 1); | |
2114 | a = &coffsymbol (sym->bsym)->native[i + 1].u.auxent; | |
2115 | if (sym->sy_tc.class == XMC_TC0) | |
2116 | { | |
2117 | /* This is the TOC table. */ | |
2118 | know (strcmp (S_GET_NAME (sym), "TOC") == 0); | |
2119 | a->x_csect.x_scnlen.l = 0; | |
2120 | a->x_csect.x_smtyp = (2 << 3) | XTY_SD; | |
2121 | } | |
2122 | else if (sym->sy_tc.subseg != 0) | |
2123 | { | |
2124 | /* This is a csect symbol. x_scnlen is the size of the | |
2125 | csect. */ | |
2126 | if (sym->sy_tc.next == (symbolS *) NULL) | |
2127 | a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput, | |
2128 | S_GET_SEGMENT (sym)) | |
2129 | - S_GET_VALUE (sym)); | |
2130 | else | |
2131 | { | |
2132 | resolve_symbol_value (sym->sy_tc.next); | |
2133 | a->x_csect.x_scnlen.l = (S_GET_VALUE (sym->sy_tc.next) | |
2134 | - S_GET_VALUE (sym)); | |
2135 | } | |
2136 | a->x_csect.x_smtyp = (sym->sy_tc.align << 3) | XTY_SD; | |
2137 | } | |
2138 | else if (S_GET_SEGMENT (sym) == bss_section) | |
2139 | { | |
2140 | /* This is a common symbol. */ | |
2141 | a->x_csect.x_scnlen.l = sym->sy_frag->fr_offset; | |
2142 | a->x_csect.x_smtyp = (sym->sy_tc.align << 3) | XTY_CM; | |
2143 | if (S_IS_EXTERNAL (sym)) | |
2144 | sym->sy_tc.class = XMC_RW; | |
2145 | else | |
2146 | sym->sy_tc.class = XMC_BS; | |
2147 | } | |
2148 | else if (! S_IS_DEFINED (sym)) | |
2149 | { | |
2150 | /* This is an external symbol. */ | |
2151 | a->x_csect.x_scnlen.l = 0; | |
2152 | a->x_csect.x_smtyp = XTY_ER; | |
2153 | } | |
2154 | else if (sym->sy_tc.class == XMC_TC) | |
2155 | { | |
2156 | symbolS *next; | |
2157 | ||
2158 | /* This is a TOC definition. x_scnlen is the size of the | |
2159 | TOC entry. */ | |
2160 | next = symbol_next (sym); | |
2161 | while (next->sy_tc.class == XMC_TC0) | |
2162 | next = symbol_next (next); | |
2163 | if (next == (symbolS *) NULL | |
2164 | || next->sy_tc.class != XMC_TC) | |
2165 | { | |
2166 | if (ppc_after_toc_frag == (fragS *) NULL) | |
2167 | a->x_csect.x_scnlen.l = (bfd_section_size (stdoutput, | |
2168 | data_section) | |
2169 | - S_GET_VALUE (sym)); | |
2170 | else | |
2171 | a->x_csect.x_scnlen.l = (ppc_after_toc_frag->fr_address | |
2172 | - S_GET_VALUE (sym)); | |
2173 | } | |
2174 | else | |
2175 | { | |
2176 | resolve_symbol_value (next); | |
2177 | a->x_csect.x_scnlen.l = (S_GET_VALUE (next) | |
2178 | - S_GET_VALUE (sym)); | |
2179 | } | |
2180 | a->x_csect.x_smtyp = (2 << 3) | XTY_SD; | |
2181 | } | |
2182 | else | |
2183 | { | |
2184 | symbolS *csect; | |
2185 | ||
2186 | /* This is a normal symbol definition. x_scnlen is the | |
2187 | symbol index of the containing csect. */ | |
2188 | if (S_GET_SEGMENT (sym) == text_section) | |
2189 | csect = ppc_text_csects; | |
2190 | else if (S_GET_SEGMENT (sym) == data_section) | |
2191 | csect = ppc_data_csects; | |
2192 | else | |
2193 | abort (); | |
2194 | ||
2195 | /* Skip the initial dummy symbol. */ | |
2196 | csect = csect->sy_tc.next; | |
2197 | ||
2198 | if (csect == (symbolS *) NULL) | |
2199 | a->x_csect.x_scnlen.l = 0; | |
2200 | else | |
2201 | { | |
2202 | while (csect->sy_tc.next != (symbolS *) NULL) | |
2203 | { | |
2204 | resolve_symbol_value (csect->sy_tc.next); | |
2205 | if (S_GET_VALUE (csect->sy_tc.next) > S_GET_VALUE (sym)) | |
2206 | break; | |
2207 | csect = csect->sy_tc.next; | |
2208 | } | |
2209 | ||
2210 | a->x_csect.x_scnlen.p = coffsymbol (csect->bsym)->native; | |
2211 | coffsymbol (sym->bsym)->native[i + 1].fix_scnlen = 1; | |
2212 | } | |
2213 | a->x_csect.x_smtyp = XTY_LD; | |
2214 | } | |
2215 | ||
2216 | a->x_csect.x_parmhash = 0; | |
2217 | a->x_csect.x_snhash = 0; | |
2218 | if (sym->sy_tc.class == -1) | |
2219 | a->x_csect.x_smclas = XMC_PR; | |
2220 | else | |
2221 | a->x_csect.x_smclas = sym->sy_tc.class; | |
2222 | a->x_csect.x_stab = 0; | |
2223 | a->x_csect.x_snstab = 0; | |
2224 | } | |
2225 | else if (S_GET_STORAGE_CLASS (sym) == C_BSTAT) | |
2226 | { | |
2227 | /* We want the value to be the symbol index of the referenced | |
2228 | csect symbol. BFD will do that for us if we set the right | |
2229 | flags. */ | |
2230 | S_SET_VALUE (sym, | |
2231 | (valueT) coffsymbol (sym->sy_tc.within->bsym)->native); | |
2232 | coffsymbol (sym->bsym)->native->fix_value = 1; | |
2233 | } | |
1eeb357e ILT |
2234 | else if (S_GET_STORAGE_CLASS (sym) == C_STSYM) |
2235 | { | |
2236 | symbolS *block; | |
2237 | symbolS *csect; | |
2238 | ||
2239 | /* The value is the offset from the enclosing csect. */ | |
2240 | block = sym->sy_tc.within; | |
2241 | csect = block->sy_tc.within; | |
2242 | resolve_symbol_value (csect); | |
2243 | S_SET_VALUE (sym, S_GET_VALUE (sym) - S_GET_VALUE (csect)); | |
2244 | } | |
882bdc69 ILT |
2245 | |
2246 | return 0; | |
2247 | } | |
2248 | ||
2249 | /* Set the VMA for a section. This is called on all the sections in | |
2250 | turn. */ | |
2251 | ||
2252 | void | |
2253 | ppc_frob_section (sec) | |
2254 | asection *sec; | |
2255 | { | |
2256 | static bfd_size_type vma = 0; | |
2257 | ||
2258 | bfd_set_section_vma (stdoutput, sec, vma); | |
2259 | vma += bfd_section_size (stdoutput, sec); | |
2260 | } | |
2261 | ||
2262 | /* Adjust the file by adding a .debug section if needed. */ | |
2263 | ||
2264 | void | |
2265 | ppc_frob_file () | |
2266 | { | |
2267 | if (ppc_debug_name_section_size > 0) | |
2268 | { | |
2269 | asection *sec; | |
2270 | ||
2271 | sec = bfd_make_section (stdoutput, ".debug"); | |
2272 | if (sec == (asection *) NULL | |
2273 | || ! bfd_set_section_size (stdoutput, sec, | |
2274 | ppc_debug_name_section_size) | |
2275 | || ! bfd_set_section_flags (stdoutput, sec, | |
2276 | SEC_HAS_CONTENTS | SEC_LOAD)) | |
2277 | as_fatal ("can't make .debug section"); | |
2278 | } | |
2279 | } | |
2280 | ||
2281 | #endif /* OBJ_COFF */ | |
2282 | \f | |
2283 | /* Turn a string in input_line_pointer into a floating point constant | |
2284 | of type type, and store the appropriate bytes in *litp. The number | |
2285 | of LITTLENUMS emitted is stored in *sizep . An error message is | |
2286 | returned, or NULL on OK. */ | |
2287 | ||
2288 | char * | |
2289 | md_atof (type, litp, sizep) | |
2290 | int type; | |
2291 | char *litp; | |
2292 | int *sizep; | |
2293 | { | |
2294 | int prec; | |
2295 | LITTLENUM_TYPE words[4]; | |
2296 | char *t; | |
2297 | int i; | |
2298 | ||
2299 | switch (type) | |
2300 | { | |
2301 | case 'f': | |
2302 | prec = 2; | |
2303 | break; | |
2304 | ||
2305 | case 'd': | |
2306 | prec = 4; | |
2307 | break; | |
2308 | ||
2309 | default: | |
2310 | *sizep = 0; | |
2311 | return "bad call to md_atof"; | |
2312 | } | |
2313 | ||
2314 | t = atof_ieee (input_line_pointer, type, words); | |
2315 | if (t) | |
2316 | input_line_pointer = t; | |
2317 | ||
2318 | *sizep = prec * 2; | |
2319 | ||
2320 | if (ppc_big_endian) | |
2321 | { | |
2322 | for (i = 0; i < prec; i++) | |
2323 | { | |
2324 | md_number_to_chars (litp, (valueT) words[i], 2); | |
2325 | litp += 2; | |
2326 | } | |
2327 | } | |
2328 | else | |
2329 | { | |
2330 | for (i = prec - 1; i >= 0; i--) | |
2331 | { | |
2332 | md_number_to_chars (litp, (valueT) words[i], 2); | |
2333 | litp += 2; | |
2334 | } | |
2335 | } | |
2336 | ||
2337 | return NULL; | |
2338 | } | |
2339 | ||
2340 | /* Write a value out to the object file, using the appropriate | |
2341 | endianness. */ | |
2342 | ||
2343 | void | |
2344 | md_number_to_chars (buf, val, n) | |
2345 | char *buf; | |
2346 | valueT val; | |
2347 | int n; | |
2348 | { | |
2349 | if (ppc_big_endian) | |
2350 | number_to_chars_bigendian (buf, val, n); | |
2351 | else | |
2352 | number_to_chars_littleendian (buf, val, n); | |
2353 | } | |
2354 | ||
2355 | /* Align a section (I don't know why this is machine dependent). */ | |
2356 | ||
2357 | valueT | |
2358 | md_section_align (seg, addr) | |
2359 | asection *seg; | |
2360 | valueT addr; | |
2361 | { | |
2362 | int align = bfd_get_section_alignment (stdoutput, seg); | |
2363 | ||
2364 | return ((addr + (1 << align) - 1) & (-1 << align)); | |
2365 | } | |
2366 | ||
2367 | /* We don't have any form of relaxing. */ | |
2368 | ||
2369 | int | |
2370 | md_estimate_size_before_relax (fragp, seg) | |
2371 | fragS *fragp; | |
2372 | asection *seg; | |
2373 | { | |
2374 | abort (); | |
2375 | } | |
2376 | ||
882bdc69 ILT |
2377 | /* Convert a machine dependent frag. We never generate these. */ |
2378 | ||
2379 | void | |
2380 | md_convert_frag (abfd, sec, fragp) | |
2381 | bfd *abfd; | |
2382 | asection *sec; | |
2383 | fragS *fragp; | |
2384 | { | |
2385 | abort (); | |
2386 | } | |
2387 | ||
882bdc69 ILT |
2388 | /* We have no need to default values of symbols. */ |
2389 | ||
2390 | /*ARGSUSED*/ | |
2391 | symbolS * | |
2392 | md_undefined_symbol (name) | |
2393 | char *name; | |
2394 | { | |
2395 | return 0; | |
2396 | } | |
2397 | \f | |
2398 | /* Functions concerning relocs. */ | |
2399 | ||
2400 | /* The location from which a PC relative jump should be calculated, | |
2401 | given a PC relative reloc. */ | |
2402 | ||
2403 | long | |
2404 | md_pcrel_from (fixp) | |
2405 | fixS *fixp; | |
2406 | { | |
2407 | #ifdef OBJ_ELF | |
2408 | if (fixp->fx_addsy != (symbolS *) NULL | |
2409 | && ! S_IS_DEFINED (fixp->fx_addsy)) | |
2410 | return 0; | |
2411 | #endif | |
2412 | ||
2413 | return fixp->fx_frag->fr_address + fixp->fx_where; | |
2414 | } | |
2415 | ||
2416 | #ifdef OBJ_COFF | |
2417 | ||
2418 | /* This is called to see whether a fixup should be adjusted to use a | |
2419 | section symbol. We take the opportunity to change a fixup against | |
2420 | a symbol in the TOC subsegment into a reloc against the | |
1eeb357e | 2421 | corresponding .tc symbol. */ |
882bdc69 ILT |
2422 | |
2423 | int | |
2424 | ppc_fix_adjustable (fix) | |
2425 | fixS *fix; | |
2426 | { | |
2427 | valueT val; | |
2428 | ||
1eeb357e ILT |
2429 | resolve_symbol_value (fix->fx_addsy); |
2430 | val = S_GET_VALUE (fix->fx_addsy); | |
882bdc69 ILT |
2431 | if (ppc_toc_csect != (symbolS *) NULL |
2432 | && fix->fx_addsy != (symbolS *) NULL | |
2433 | && fix->fx_addsy != ppc_toc_csect | |
2434 | && S_GET_SEGMENT (fix->fx_addsy) == data_section | |
2435 | && val >= ppc_toc_frag->fr_address | |
2436 | && (ppc_after_toc_frag == (fragS *) NULL | |
2437 | || val < ppc_after_toc_frag->fr_address)) | |
2438 | { | |
2439 | symbolS *sy; | |
2440 | ||
2441 | for (sy = symbol_next (ppc_toc_csect); | |
2442 | sy != (symbolS *) NULL; | |
2443 | sy = symbol_next (sy)) | |
2444 | { | |
2445 | if (sy->sy_tc.class == XMC_TC0) | |
2446 | continue; | |
2447 | if (sy->sy_tc.class != XMC_TC) | |
2448 | break; | |
1eeb357e ILT |
2449 | resolve_symbol_value (sy); |
2450 | if (val == S_GET_VALUE (sy)) | |
882bdc69 ILT |
2451 | { |
2452 | fix->fx_addsy = sy; | |
2453 | fix->fx_addnumber = val - ppc_toc_frag->fr_address; | |
2454 | return 0; | |
2455 | } | |
2456 | } | |
2457 | ||
2458 | as_bad_where (fix->fx_file, fix->fx_line, | |
2459 | "symbol in .toc does not match any .tc"); | |
2460 | } | |
2461 | ||
2462 | /* Possibly adjust the reloc to be against the csect. */ | |
2463 | if (fix->fx_addsy != (symbolS *) NULL | |
2464 | && fix->fx_addsy->sy_tc.subseg == 0 | |
2465 | && fix->fx_addsy->sy_tc.class != XMC_TC0 | |
2466 | && fix->fx_addsy->sy_tc.class != XMC_TC | |
2467 | && S_GET_SEGMENT (fix->fx_addsy) != bss_section) | |
2468 | { | |
2469 | symbolS *csect; | |
2470 | ||
2471 | if (S_GET_SEGMENT (fix->fx_addsy) == text_section) | |
2472 | csect = ppc_text_csects; | |
2473 | else if (S_GET_SEGMENT (fix->fx_addsy) == data_section) | |
2474 | csect = ppc_data_csects; | |
2475 | else | |
2476 | abort (); | |
2477 | ||
2478 | /* Skip the initial dummy symbol. */ | |
2479 | csect = csect->sy_tc.next; | |
2480 | ||
2481 | if (csect != (symbolS *) NULL) | |
2482 | { | |
2483 | while (csect->sy_tc.next != (symbolS *) NULL | |
2484 | && (csect->sy_tc.next->sy_frag->fr_address | |
2485 | <= fix->fx_addsy->sy_frag->fr_address)) | |
2486 | csect = csect->sy_tc.next; | |
2487 | ||
2488 | fix->fx_offset += (S_GET_VALUE (fix->fx_addsy) | |
1eeb357e | 2489 | - csect->sy_frag->fr_address); |
882bdc69 ILT |
2490 | fix->fx_addsy = csect; |
2491 | } | |
2492 | } | |
2493 | ||
2494 | /* Adjust a reloc against a .lcomm symbol to be against the base | |
2495 | .lcomm. */ | |
2496 | if (fix->fx_addsy != (symbolS *) NULL | |
2497 | && S_GET_SEGMENT (fix->fx_addsy) == bss_section | |
2498 | && ! S_IS_EXTERNAL (fix->fx_addsy)) | |
2499 | { | |
1eeb357e ILT |
2500 | resolve_symbol_value (fix->fx_addsy->sy_frag->fr_symbol); |
2501 | fix->fx_offset += (S_GET_VALUE (fix->fx_addsy) | |
2502 | - S_GET_VALUE (fix->fx_addsy->sy_frag->fr_symbol)); | |
882bdc69 ILT |
2503 | fix->fx_addsy = fix->fx_addsy->sy_frag->fr_symbol; |
2504 | } | |
2505 | ||
2506 | return 0; | |
2507 | } | |
2508 | ||
2509 | #endif | |
2510 | ||
2511 | /* See whether a symbol is in the TOC section. */ | |
2512 | ||
2513 | static int | |
2514 | ppc_is_toc_sym (sym) | |
2515 | symbolS *sym; | |
2516 | { | |
2517 | #ifdef OBJ_COFF | |
2518 | return sym->sy_tc.class == XMC_TC; | |
2519 | #else | |
2520 | return strcmp (segment_name (S_GET_SEGMENT (sym)), ".got") == 0; | |
2521 | #endif | |
2522 | } | |
2523 | ||
2524 | /* Apply a fixup to the object code. This is called for all the | |
2525 | fixups we generated by the call to fix_new_exp, above. In the call | |
2526 | above we used a reloc code which was the largest legal reloc code | |
2527 | plus the operand index. Here we undo that to recover the operand | |
2528 | index. At this point all symbol values should be fully resolved, | |
2529 | and we attempt to completely resolve the reloc. If we can not do | |
2530 | that, we determine the correct reloc code and put it back in the | |
2531 | fixup. */ | |
2532 | ||
2533 | int | |
3f81f3cf | 2534 | md_apply_fix3 (fixp, valuep, seg) |
882bdc69 ILT |
2535 | fixS *fixp; |
2536 | valueT *valuep; | |
3f81f3cf | 2537 | segT seg; |
882bdc69 ILT |
2538 | { |
2539 | valueT value; | |
2540 | ||
2541 | /* FIXME FIXME FIXME: The value we are passed in *valuep includes | |
2542 | the symbol values. Since we are using BFD_ASSEMBLER, if we are | |
2543 | doing this relocation the code in write.c is going to call | |
2544 | bfd_perform_relocation, which is also going to use the symbol | |
2545 | value. That means that if the reloc is fully resolved we want to | |
2546 | use *valuep since bfd_perform_relocation is not being used. | |
2547 | However, if the reloc is not fully resolved we do not want to use | |
2548 | *valuep, and must use fx_offset instead. However, if the reloc | |
2549 | is PC relative, we do want to use *valuep since it includes the | |
2550 | result of md_pcrel_from. This is confusing. */ | |
2551 | ||
2552 | if (fixp->fx_addsy == (symbolS *) NULL) | |
2553 | { | |
2554 | value = *valuep; | |
2555 | fixp->fx_done = 1; | |
2556 | } | |
2557 | else if (fixp->fx_pcrel) | |
2558 | value = *valuep; | |
2559 | else | |
2560 | { | |
2561 | value = fixp->fx_offset; | |
2562 | if (fixp->fx_subsy != (symbolS *) NULL) | |
2563 | { | |
2564 | if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section) | |
2565 | value -= S_GET_VALUE (fixp->fx_subsy); | |
2566 | else | |
2567 | { | |
2568 | /* We can't actually support subtracting a symbol. */ | |
2569 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
2570 | "expression too complex"); | |
2571 | } | |
2572 | } | |
2573 | } | |
2574 | ||
2575 | if ((int) fixp->fx_r_type >= (int) BFD_RELOC_UNUSED) | |
2576 | { | |
2577 | int opindex; | |
2578 | const struct powerpc_operand *operand; | |
2579 | char *where; | |
2580 | unsigned long insn; | |
2581 | ||
2582 | opindex = (int) fixp->fx_r_type - (int) BFD_RELOC_UNUSED; | |
2583 | ||
2584 | operand = &powerpc_operands[opindex]; | |
2585 | ||
1eeb357e ILT |
2586 | #ifdef OBJ_COFF |
2587 | /* It appears that an instruction like | |
2588 | l 9,LC..1(30) | |
2589 | when LC..1 is not a TOC symbol does not generate a reloc. It | |
2590 | uses the offset of LC..1 within its csect. However, .long | |
2591 | LC..1 will generate a reloc. I can't find any documentation | |
2592 | on how these cases are to be distinguished, so this is a wild | |
2593 | guess. These cases are generated by gcc -mminimal-toc. */ | |
2594 | if ((operand->flags & PPC_OPERAND_PARENS) != 0 | |
2595 | && operand->bits == 16 | |
2596 | && operand->shift == 0 | |
2597 | && operand->insert == NULL | |
2598 | && fixp->fx_addsy != NULL | |
2599 | && fixp->fx_addsy->sy_tc.subseg != 0 | |
2600 | && fixp->fx_addsy->sy_tc.class != XMC_TC | |
2601 | && fixp->fx_addsy->sy_tc.class != XMC_TC0 | |
2602 | && S_GET_SEGMENT (fixp->fx_addsy) != bss_section) | |
2603 | { | |
2604 | value = fixp->fx_offset; | |
2605 | fixp->fx_done = 1; | |
2606 | } | |
2607 | #endif | |
2608 | ||
882bdc69 ILT |
2609 | /* Fetch the instruction, insert the fully resolved operand |
2610 | value, and stuff the instruction back again. */ | |
2611 | where = fixp->fx_frag->fr_literal + fixp->fx_where; | |
2612 | if (ppc_big_endian) | |
2613 | insn = bfd_getb32 ((unsigned char *) where); | |
2614 | else | |
2615 | insn = bfd_getl32 ((unsigned char *) where); | |
2616 | insn = ppc_insert_operand (insn, operand, (offsetT) value, | |
2617 | fixp->fx_file, fixp->fx_line); | |
2618 | if (ppc_big_endian) | |
2619 | bfd_putb32 ((bfd_vma) insn, (unsigned char *) where); | |
2620 | else | |
2621 | bfd_putl32 ((bfd_vma) insn, (unsigned char *) where); | |
2622 | ||
2623 | if (fixp->fx_done) | |
2624 | { | |
2625 | /* Nothing else to do here. */ | |
2626 | return 1; | |
2627 | } | |
2628 | ||
2629 | /* Determine a BFD reloc value based on the operand information. | |
2630 | We are only prepared to turn a few of the operands into | |
2631 | relocs. | |
2632 | FIXME: We need to handle the DS field at the very least. | |
2633 | FIXME: Handling 16 bit branches would also be reasonable. | |
2634 | FIXME: Selecting the reloc type is a bit haphazard; perhaps | |
2635 | there should be a new field in the operand table. */ | |
2636 | if ((operand->flags & PPC_OPERAND_RELATIVE) != 0 | |
2637 | && operand->bits == 26 | |
2638 | && operand->shift == 0) | |
2639 | fixp->fx_r_type = BFD_RELOC_PPC_B26; | |
2640 | else if ((operand->flags & PPC_OPERAND_ABSOLUTE) != 0 | |
2641 | && operand->bits == 26 | |
2642 | && operand->shift == 0) | |
2643 | fixp->fx_r_type = BFD_RELOC_PPC_BA26; | |
2644 | else if ((operand->flags & PPC_OPERAND_PARENS) != 0 | |
2645 | && operand->bits == 16 | |
2646 | && operand->shift == 0 | |
2647 | && operand->insert == NULL | |
2648 | && fixp->fx_addsy != NULL | |
2649 | && ppc_is_toc_sym (fixp->fx_addsy)) | |
2650 | { | |
2651 | fixp->fx_size = 2; | |
2652 | if (ppc_big_endian) | |
2653 | fixp->fx_where += 2; | |
2654 | fixp->fx_r_type = BFD_RELOC_PPC_TOC16; | |
2655 | } | |
2656 | else | |
2657 | { | |
2658 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
2659 | "unresolved expression that must be resolved"); | |
2660 | fixp->fx_done = 1; | |
2661 | return 1; | |
2662 | } | |
2663 | } | |
2664 | else | |
2665 | { | |
3f81f3cf MM |
2666 | #ifdef OBJ_ELF |
2667 | ppc_elf_validate_fix (fixp, seg); | |
2668 | #endif | |
882bdc69 ILT |
2669 | switch (fixp->fx_r_type) |
2670 | { | |
2671 | case BFD_RELOC_32: | |
3f81f3cf MM |
2672 | if (fixp->fx_pcrel) |
2673 | { | |
2674 | fixp->fx_r_type = BFD_RELOC_32_PCREL; | |
2675 | value += fixp->fx_frag->fr_address + fixp->fx_where; | |
2676 | } /* fall through */ | |
2677 | ||
4a6b2f8b | 2678 | case BFD_RELOC_32_PCREL: |
882bdc69 ILT |
2679 | md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where, |
2680 | value, 4); | |
2681 | break; | |
4a6b2f8b MM |
2682 | case BFD_RELOC_LO16: |
2683 | case BFD_RELOC_HI16: | |
2684 | case BFD_RELOC_HI16_S: | |
2685 | case BFD_RELOC_PPC_TOC16: | |
882bdc69 | 2686 | case BFD_RELOC_16: |
3f81f3cf MM |
2687 | if (fixp->fx_pcrel) |
2688 | abort (); | |
2689 | ||
882bdc69 ILT |
2690 | md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where, |
2691 | value, 2); | |
2692 | break; | |
3f81f3cf | 2693 | |
882bdc69 | 2694 | case BFD_RELOC_8: |
3f81f3cf MM |
2695 | if (fixp->fx_pcrel) |
2696 | abort (); | |
2697 | ||
882bdc69 ILT |
2698 | md_number_to_chars (fixp->fx_frag->fr_literal + fixp->fx_where, |
2699 | value, 1); | |
2700 | break; | |
2701 | default: | |
2702 | abort (); | |
2703 | } | |
2704 | } | |
2705 | ||
2706 | #ifdef OBJ_ELF | |
2707 | fixp->fx_addnumber = value; | |
2708 | #else | |
2709 | if (fixp->fx_r_type != BFD_RELOC_PPC_TOC16) | |
2710 | fixp->fx_addnumber = 0; | |
2711 | else | |
2712 | { | |
2713 | /* We want to use the offset within the data segment of the | |
2714 | symbol, not the actual VMA of the symbol. */ | |
2715 | fixp->fx_addnumber = | |
2716 | - bfd_get_section_vma (stdoutput, S_GET_SEGMENT (fixp->fx_addsy)); | |
2717 | } | |
2718 | #endif | |
2719 | ||
2720 | return 1; | |
2721 | } | |
2722 | ||
2723 | /* Generate a reloc for a fixup. */ | |
2724 | ||
2725 | arelent * | |
2726 | tc_gen_reloc (seg, fixp) | |
2727 | asection *seg; | |
2728 | fixS *fixp; | |
2729 | { | |
2730 | arelent *reloc; | |
2731 | ||
2732 | reloc = (arelent *) bfd_alloc_by_size_t (stdoutput, sizeof (arelent)); | |
2733 | ||
2734 | reloc->sym_ptr_ptr = &fixp->fx_addsy->bsym; | |
2735 | reloc->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
2736 | reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); | |
2737 | if (reloc->howto == (reloc_howto_type *) NULL) | |
2738 | { | |
2739 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
4a6b2f8b | 2740 | "reloc %d not supported by object file format", (int)fixp->fx_r_type); |
882bdc69 ILT |
2741 | return NULL; |
2742 | } | |
2743 | reloc->addend = fixp->fx_addnumber; | |
2744 | ||
882bdc69 ILT |
2745 | return reloc; |
2746 | } |