1 /* obj-evax.c - EVAX (openVMS/Alpha) object file format.
2 Copyright 1996, 1997, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Klaus Kämpf (kkaempf@progis.de) of
4 proGIS Software, Aachen, Germany.
5 Extensively enhanced by Douglas Rupp of AdaCore.
7 This file is part of GAS, the GNU Assembler
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GAS; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 #define OBJ_HEADER "obj-evax.h"
30 #include "struc-symbol.h"
31 #include "safe-ctype.h"
33 static void s_evax_weak (int);
34 static unsigned int crc32 (unsigned char *, int);
35 static char *encode_32 (unsigned int);
36 static char *encode_16 (unsigned int);
37 static int decode_16 (const char *);
39 const pseudo_typeS obj_pseudo_table
[] =
41 { "weak", s_evax_weak
, 0},
43 }; /* obj_pseudo_table */
45 void obj_read_begin_hook () {}
47 /* Handle the weak specific pseudo-op. */
50 s_evax_weak (int ignore ATTRIBUTE_UNUSED
)
59 stop
= mri_comment_field (&stopc
);
63 name
= input_line_pointer
;
64 c
= get_symbol_end ();
65 symbolP
= symbol_find_or_make (name
);
66 *input_line_pointer
= c
;
73 if (*input_line_pointer
== '\n')
80 mri_comment_end (stop
, stopc
);
82 demand_empty_rest_of_line ();
86 evax_symbol_new_hook (symbolS
*sym
)
88 struct evax_private_udata_struct
*udata
;
90 udata
= (struct evax_private_udata_struct
*)
91 xmalloc (sizeof (struct evax_private_udata_struct
));
93 udata
->bsym
= symbol_get_bfdsym (sym
);
95 udata
->origname
= xstrdup (S_GET_NAME (sym
));
97 symbol_get_bfdsym(sym
)->udata
.p
= (PTR
) udata
;
101 evax_frob_symbol (symbolS
*sym
, int *punt
)
103 const char *symname
= S_GET_NAME (sym
);
104 int symlen
= strlen (symname
);
105 asymbol
*symbol
= symbol_get_bfdsym (sym
);
108 && strcmp (symname
+ symlen
- 4, "..en") == 0
109 && S_GET_SEGMENT (sym
) == undefined_section
)
111 symbol_clear_used_in_reloc (sym
);
115 else if ((symbol
->flags
& BSF_GLOBAL
) && (symbol
->flags
& BSF_FUNCTION
))
117 struct evax_private_udata_struct
*udata
118 = (struct evax_private_udata_struct
*)symbol
->udata
.p
;
120 /* Fix up equates of function definitions. */
121 while (udata
->enbsym
== NULL
)
123 /* ??? Equates have been resolved at this point so their
124 expression is O_constant; but they previously were
125 O_symbol and we hope the equated symbol is still there. */
126 sym
= symbol_get_value_expression (sym
)->X_add_symbol
;
129 as_bad (_("no entry symbol for global function '%s'"), symname
);
132 symbol
= symbol_get_bfdsym (sym
);
134 = ((struct evax_private_udata_struct
*)symbol
->udata
.p
)->enbsym
;
140 evax_frob_file_before_adjust (void)
142 struct alpha_linkage_fixups
*l
;
143 segT current_section
= now_seg
;
144 int current_subsec
= now_subseg
;
145 segment_info_type
*seginfo
;
146 int linkage_index
= 1;
148 subseg_set (alpha_link_section
, 0);
149 seginfo
= seg_info (alpha_link_section
);
151 /* Handle .linkage fixups. */
152 for (l
= alpha_linkage_fixup_root
; l
!= NULL
; l
= l
->next
)
154 if (S_GET_SEGMENT (l
->fixp
->fx_addsy
) == alpha_link_section
)
157 fixS
*fixpentry
, *fixppdesc
, *fixtail
;
159 fixtail
= seginfo
->fix_tail
;
161 /* Replace the linkage with the local symbols */
162 entry_sym
= symbol_find
163 (((struct evax_private_udata_struct
*)symbol_get_bfdsym (l
->fixp
->fx_addsy
)->udata
.p
)->enbsym
->name
);
166 fixpentry
= fix_new (l
->fixp
->fx_frag
, l
->fixp
->fx_where
, 8,
167 entry_sym
, l
->fixp
->fx_offset
, 0,
169 fixppdesc
= fix_new (l
->fixp
->fx_frag
, l
->fixp
->fx_where
+8, 8,
170 l
->fixp
->fx_addsy
, l
->fixp
->fx_offset
, 0,
172 l
->fixp
->fx_size
= 0;
173 l
->fixp
->fx_done
= 1;
175 /* If not already at the tail, splice the new fixups into
176 the chain right after the one we are nulling out */
177 if (fixtail
!= l
->fixp
)
179 fixppdesc
->fx_next
= l
->fixp
->fx_next
;
180 l
->fixp
->fx_next
= fixpentry
;
181 fixtail
->fx_next
= 0;
182 seginfo
->fix_tail
= fixtail
;
187 ((struct evax_private_udata_struct
*)
188 symbol_get_bfdsym (l
->label
)->udata
.p
)->lkindex
= linkage_index
;
190 l
->fixp
->fx_addnumber
= linkage_index
;
196 subseg_set (current_section
, current_subsec
);
200 evax_frob_file_before_fix (void)
202 /* Now that the fixups are done earlier, we need to transfer the values
203 into the BFD symbols before calling fix_segment (ideally should not
204 be done also later). */
209 /* Set the value into the BFD symbol. Up til now the value
210 has only been kept in the gas symbolS struct. */
211 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
212 symbol_get_bfdsym (symp
)->value
= S_GET_VALUE (symp
);
216 /* The length is computed from the maximum allowable length of 64 less the
217 4 character ..xx extension that must be preserved (removed before
218 krunching and appended back on afterwards). The $<nnn>.. prefix is
219 also removed and prepened back on, but doesn't enter into the length
220 computation because symbols with that prefix are always resolved
221 by the assembler and will never appear in the symbol table. At least
222 I hope that's true, TBD. */
223 #define MAX_LABEL_LENGTH 60
225 static char *shorten_identifier (char *);
226 static int is_truncated_identifier (char *);
229 evax_shorten_name (char *id
)
231 int prefix_dotdot
= 0;
233 int len
= strlen (id
);
234 int suffix_dotdot
= len
;
238 /* This test may be too conservative. */
239 if (len
<= MAX_LABEL_LENGTH
)
245 /* Check for ..xx suffix and save it. */
246 if (strncmp (&id
[len
-4], "..", 2) == 0)
248 suffix_dotdot
= len
- 4;
249 strncpy (suffix
, &id
[len
-4], 4);
253 /* Check for $<nnn>.. prefix and save it. */
254 if ((id
[0] == '$') && ISDIGIT (id
[1]))
258 for (i
=2; i
< len
; i
++)
260 if (!ISDIGIT (id
[i
]))
262 if (id
[i
] == '.' && id
[i
+1] == '.')
265 strncpy (prefix
, id
, prefix_dotdot
);
266 prefix
[prefix_dotdot
] = 0;
273 /* We only need worry about krunching the base symbol. */
274 base_id
= xmalloc (suffix_dotdot
- prefix_dotdot
+ 1);
275 strncpy (base_id
, &id
[prefix_dotdot
], suffix_dotdot
- prefix_dotdot
);
276 base_id
[suffix_dotdot
- prefix_dotdot
] = 0;
278 if (strlen (base_id
) > MAX_LABEL_LENGTH
)
283 strcpy (new_id
, base_id
);
286 strcpy (new_id
, shorten_identifier (new_id
));
288 /* Prepend back the prefix if there was one. */
291 memmove (&new_id
[prefix_dotdot
], new_id
, strlen (new_id
) + 1);
292 strncpy (new_id
, prefix
, prefix_dotdot
);
295 /* Append back the suffix if there was one. */
297 strcat (new_id
, suffix
);
299 /* Save it on the heap and return. */
300 return_id
= xmalloc (strlen (new_id
) + 1);
301 strcpy (return_id
, new_id
);
309 /* The code below implements a mechanism for truncating long
310 identifiers to an arbitrary length (set by MAX_LABEL_LENGTH).
312 It attempts to make each truncated identifier unique by replacing
313 part of the identifier with an encoded 32-bit CRC and an associated
314 checksum (the checksum is used as a way to determine that the name
317 Note that both a portion of the start and of the end of the
318 identifier may be kept. The macro ID_SUFFIX_LENGTH will return the
319 number of characters in the suffix of the identifier that should be
322 The portion of the identifier that is going to be removed is
323 checksummed. The checksum is then encoded as a 5-character string,
324 the characters of which are then summed. This sum is then encoded
325 as a 3-character string. Finally, the original length of the
326 identifier is encoded as a 3-character string.
328 These three strings are then concatenated together (along with an _h
329 which further designates that the name was truncated):
331 "original_identifier"_haaaaabbbccc
334 bbb = length of original identifier
335 ccc = sum of 32-bit CRC characters
337 The resulting identifier will be MAX_LABEL_LENGTH characters long.
342 /* Table used to convert an integer into a string. */
344 static const char codings
[] = {
345 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
346 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
347 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
348 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
349 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_'};
351 /* The number of codings in the above table. */
352 static const int number_of_codings
= sizeof (codings
) / sizeof (char);
354 /* Table used by decode_16 () to convert an encoded string back into
356 static char decodings
[256];
358 /* Table used by the crc32 function to calcuate the checksum. */
359 static unsigned int crc32_table
[256] = {0, 0};
361 /* Given a string in BUF, calculate a 32-bit CRC for it.
363 This is used as a reasonably unique hash for the given string. */
366 crc32 (unsigned char *buf
, int len
)
368 unsigned int crc
= 0xffffffff;
370 if (! crc32_table
[1])
372 /* Initialize the CRC table and the decoding table. */
376 for (i
= 0; i
< 256; i
++)
378 for (c
= i
<< 24, j
= 8; j
> 0; --j
)
379 c
= c
& 0x80000000 ? (c
<< 1) ^ 0x04c11db7 : (c
<< 1);
383 for (i
= 0; i
< number_of_codings
; i
++)
384 decodings
[codings
[i
] & 255] = i
;
389 crc
= (crc
<< 8) ^ crc32_table
[(crc
>> 24) ^ *buf
];
395 /* Encode the lower 32 bits of VALUE as a 5-character string. */
398 encode_32 (unsigned int value
)
404 for(x
= 0; x
< 5; x
++)
406 res
[x
] = codings
[value
% number_of_codings
];
407 value
= value
/ number_of_codings
;
412 /* Encode the lower 16 bits of VALUE as a 3-character string. */
415 encode_16 (unsigned int value
)
421 for(x
= 0; x
< 3; x
++)
423 res
[x
] = codings
[value
% number_of_codings
];
424 value
= value
/ number_of_codings
;
429 /* Convert the encoded string obtained from encode_16 () back into a
433 decode_16 (const char *string
)
435 return decodings
[(int) string
[2]] * number_of_codings
* number_of_codings
436 + decodings
[(int) string
[1]] * number_of_codings
437 + decodings
[(int) string
[0]];
440 /* ID_SUFFIX_LENGTH is used to determine how many characters in the
441 suffix of the identifier are to be preserved, if any. */
443 #ifndef ID_SUFFIX_LENGTH
444 #define ID_SUFFIX_LENGTH(ID) (0)
447 /* Return a reasonably-unique version of NAME that is less than or
448 equal to MAX_LABEL_LENGTH characters long. The string returned from
449 this function may be a copy of NAME; the function will never
450 actually modify the contents of NAME. */
452 static char newname
[MAX_LABEL_LENGTH
+ 1];
455 shorten_identifier (char *name
)
457 int crc
, len
, sum
, x
, final_len
;
459 int suffix_length
= ID_SUFFIX_LENGTH (name
);
461 if ((len
= strlen (name
)) <= MAX_LABEL_LENGTH
)
464 final_len
= MAX_LABEL_LENGTH
- 2 - 5 - 3 - 3 - suffix_length
;
465 crc
= crc32 ((unsigned char *)name
+ final_len
,
466 len
- final_len
- suffix_length
);
467 crc_chars
= encode_32 (crc
);
469 for (x
= 0; x
< 5; x
++)
470 sum
+= crc_chars
[x
];
471 strncpy (newname
, name
, final_len
);
472 newname
[MAX_LABEL_LENGTH
] = 0;
473 /* Now append the suffix of the original identifier, if any. */
475 strncpy (newname
+ MAX_LABEL_LENGTH
- suffix_length
,
476 name
+ len
- suffix_length
,
478 strncpy (newname
+ final_len
, "_h", 2);
479 strncpy (newname
+ final_len
+ 2 , crc_chars
, 5);
480 strncpy (newname
+ final_len
+ 2 + 5, encode_16 (len
), 3);
481 strncpy (newname
+ final_len
+ 2 + 5 + 3, encode_16 (sum
), 3);
482 if (!is_truncated_identifier (newname
))
487 /* Determine whether or not ID is a truncated identifier, and return a
488 non-zero value if it is. */
491 is_truncated_identifier (char *id
)
494 int len
= strlen (id
);
495 /* If it's not exactly MAX_LABEL_LENGTH characters long, it can't be
496 a truncated identifier. */
497 if (len
!= MAX_LABEL_LENGTH
)
500 /* Start scanning backwards for a _h. */
501 len
= len
- 3 - 3 - 5 - 2;
505 if (ptr
[0] == '_' && ptr
[1] == 'h')
507 /* Now see if the sum encoded in the identifer matches. */
510 for (x
= 0; x
< 5; x
++)
512 /* If it matches, this is probably a truncated identifier. */
513 if (sum
== decode_16 (ptr
+ 5 + 2 + 3))
521 /* end of obj-evax.c */