]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/sunos.c
* objcopy.c (strip_main, copy_main): Add missing 'break' in switch.
[thirdparty/binutils-gdb.git] / bfd / sunos.c
CommitLineData
0ee75d02
ILT
1/* BFD backend for SunOS binaries.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4a81b561 4
0ee75d02 5This file is part of BFD, the Binary File Descriptor library.
4a81b561 6
0ee75d02 7This program is free software; you can redistribute it and/or modify
4a81b561 8it under the terms of the GNU General Public License as published by
0ee75d02
ILT
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
4a81b561 11
0ee75d02 12This program is distributed in the hope that it will be useful,
4a81b561
DHW
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
0ee75d02
ILT
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
4a81b561 20
0ee75d02
ILT
21#define ARCH 32
22#define TARGETNAME "a.out-sunos-big"
23#define MY(OP) CAT(sunos_big_,OP)
4a81b561 24
4a81b561 25#include "bfd.h"
78aa64b1 26
0ee75d02 27/* Static routines defined in this file. */
4a81b561 28
0ee75d02 29struct external_nlist;
4a81b561 30
0ee75d02
ILT
31static boolean sunos_read_dynamic_info PARAMS ((bfd *));
32static bfd_size_type MY(read_dynamic_symbols)
33 PARAMS ((bfd *, struct external_nlist **, char **, bfd_size_type *));
34static bfd_size_type MY(read_dynamic_relocs) PARAMS ((bfd *, PTR *));
4a81b561 35
0ee75d02
ILT
36#define MY_read_dynamic_symbols MY(read_dynamic_symbols)
37#define MY_read_dynamic_relocs MY(read_dynamic_relocs)
4a81b561 38
0ee75d02
ILT
39/* Include the usual a.out support. */
40#include "aoutf1.h"
4a81b561 41
0ee75d02
ILT
42/* SunOS shared library support. We store a pointer to this structure
43 in obj_aout_dynamic_info (abfd). */
4a81b561 44
0ee75d02 45struct sunos_dynamic_info
78aa64b1 46{
0ee75d02
ILT
47 /* Whether we found any dynamic information. */
48 boolean valid;
49 /* Dynamic information. */
50 struct internal_sun4_dynamic_link dyninfo;
51 /* Number of dynamic symbols. */
52 bfd_size_type dynsym_count;
53 /* Read in nlists for dynamic symbols. */
54 struct external_nlist *dynsym;
55 /* Read in dynamic string table. */
56 char *dynstr;
57 /* Number of dynamic relocs. */
58 bfd_size_type dynrel_count;
59 /* Read in dynamic relocs. This may be reloc_std_external or
60 reloc_ext_external. */
61 PTR dynrel;
62};
4a81b561 63
0ee75d02
ILT
64/* Read in the basic dynamic information. This locates the __DYNAMIC
65 structure and uses it to find the dynamic_link structure. It
66 creates and saves a sunos_dynamic_info structure. If it can't find
67 __DYNAMIC, it sets the valid field of the sunos_dynamic_info
68 structure to false to avoid doing this work again. */
4a81b561 69
0ee75d02
ILT
70static boolean
71sunos_read_dynamic_info (abfd)
4a81b561
DHW
72 bfd *abfd;
73{
0ee75d02
ILT
74 struct sunos_dynamic_info *info;
75 struct external_nlist dynsym;
76 char buf[sizeof "__DYNAMIC"];
77 asection *dynsec;
78 file_ptr dynoff;
79 struct external_sun4_dynamic dyninfo;
80 unsigned long dynver;
81 struct external_sun4_dynamic_link linkinfo;
82
83 if (obj_aout_dynamic_info (abfd) != (PTR) NULL)
84 return true;
85
86 info = ((struct sunos_dynamic_info *)
87 bfd_zalloc (abfd, sizeof (struct sunos_dynamic_info)));
88 info->valid = false;
89 info->dynsym = NULL;
90 info->dynstr = NULL;
91 info->dynrel = NULL;
92 obj_aout_dynamic_info (abfd) = (PTR) info;
93
94 /* We look for the __DYNAMIC symbol to locate the dynamic linking
95 information. It should be the first symbol if it is defined. If
96 we can't find it, don't sweat it. */
97 if ((abfd->flags & DYNAMIC) == 0
98 || bfd_get_symcount (abfd) <= 0
99 || bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
100 || (bfd_read ((PTR) &dynsym, 1, EXTERNAL_NLIST_SIZE, abfd)
101 != EXTERNAL_NLIST_SIZE)
102 || ((dynsym.e_type[0] & N_TYPE) != N_DATA
103 && (dynsym.e_type[0] & N_TYPE) != N_TEXT)
104 || bfd_seek (abfd,
105 obj_str_filepos (abfd) + GET_WORD (abfd, dynsym.e_strx),
106 SEEK_SET) != 0
107 || bfd_read ((PTR) buf, 1, sizeof buf, abfd) != sizeof buf
108 || buf[sizeof buf - 1] != '\0'
109 || strcmp (buf, "__DYNAMIC") != 0)
110 return true;
111
112 if ((dynsym.e_type[0] & N_TYPE) == N_DATA)
113 dynsec = obj_datasec (abfd);
114 else
115 dynsec = obj_textsec (abfd);
116 if (! bfd_get_section_contents (abfd, dynsec, (PTR) &dyninfo,
117 (GET_WORD (abfd, dynsym.e_value)
118 - bfd_get_section_vma (abfd, dynsec)),
119 sizeof dyninfo))
120 return true;
121
122 dynver = GET_WORD (abfd, dyninfo.ld_version);
123 if (dynver != 2 && dynver != 3)
124 return true;
125
126 dynoff = GET_WORD (abfd, dyninfo.ld);
127
128 /* dynoff is a virtual address. It is probably always in the .data
129 section, but this code should work even if it moves. */
130 if (dynoff < bfd_get_section_vma (abfd, obj_datasec (abfd)))
131 dynsec = obj_textsec (abfd);
132 else
133 dynsec = obj_datasec (abfd);
134 dynoff -= bfd_get_section_vma (abfd, dynsec);
135 if (dynoff < 0 || dynoff > bfd_section_size (abfd, dynsec))
136 return true;
137
138 /* This executable appears to be dynamically linked in a way that we
139 can understand. */
140 if (! bfd_get_section_contents (abfd, dynsec, (PTR) &linkinfo, dynoff,
141 (bfd_size_type) sizeof linkinfo))
142 return true;
143
144 /* Swap in the dynamic link information. */
145 info->dyninfo.ld_loaded = GET_WORD (abfd, linkinfo.ld_loaded);
146 info->dyninfo.ld_need = GET_WORD (abfd, linkinfo.ld_need);
147 info->dyninfo.ld_rules = GET_WORD (abfd, linkinfo.ld_rules);
148 info->dyninfo.ld_got = GET_WORD (abfd, linkinfo.ld_got);
149 info->dyninfo.ld_plt = GET_WORD (abfd, linkinfo.ld_plt);
150 info->dyninfo.ld_rel = GET_WORD (abfd, linkinfo.ld_rel);
151 info->dyninfo.ld_hash = GET_WORD (abfd, linkinfo.ld_hash);
152 info->dyninfo.ld_stab = GET_WORD (abfd, linkinfo.ld_stab);
153 info->dyninfo.ld_stab_hash = GET_WORD (abfd, linkinfo.ld_stab_hash);
154 info->dyninfo.ld_buckets = GET_WORD (abfd, linkinfo.ld_buckets);
155 info->dyninfo.ld_symbols = GET_WORD (abfd, linkinfo.ld_symbols);
156 info->dyninfo.ld_symb_size = GET_WORD (abfd, linkinfo.ld_symb_size);
157 info->dyninfo.ld_text = GET_WORD (abfd, linkinfo.ld_text);
158 info->dyninfo.ld_plt_sz = GET_WORD (abfd, linkinfo.ld_plt_sz);
159
160 /* The only way to get the size of the symbol information appears to
161 be to determine the distance between it and the string table. */
162 info->dynsym_count = ((info->dyninfo.ld_symbols - info->dyninfo.ld_stab)
163 / EXTERNAL_NLIST_SIZE);
164 BFD_ASSERT (info->dynsym_count * EXTERNAL_NLIST_SIZE
165 == info->dyninfo.ld_symbols - info->dyninfo.ld_stab);
166
167 /* Similarly, the relocs end at the hash table. */
168 info->dynrel_count = ((info->dyninfo.ld_hash - info->dyninfo.ld_rel)
169 / obj_reloc_entry_size (abfd));
170 BFD_ASSERT (info->dynrel_count * obj_reloc_entry_size (abfd)
171 == info->dyninfo.ld_hash - info->dyninfo.ld_rel);
172
173 info->valid = true;
4a81b561
DHW
174
175 return true;
176}
177
0ee75d02 178/* Read in the dynamic symbols. */
4a81b561 179
0ee75d02
ILT
180static bfd_size_type
181MY(read_dynamic_symbols) (abfd, syms, strs, strsize)
4a81b561 182 bfd *abfd;
0ee75d02
ILT
183 struct external_nlist **syms;
184 char **strs;
185 bfd_size_type *strsize;
4a81b561 186{
0ee75d02 187 struct sunos_dynamic_info *info;
4a81b561 188
0ee75d02
ILT
189 if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
190 {
191 if (! sunos_read_dynamic_info (abfd))
192 return (bfd_size_type) -1;
4a81b561 193 }
c93595dd 194
0ee75d02
ILT
195 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
196 if (! info->valid || info->dynsym_count == 0)
197 return 0;
10be52bf 198
0ee75d02
ILT
199 if (info->dynsym == (struct external_nlist *) NULL)
200 {
201 info->dynsym = ((struct external_nlist *)
202 bfd_alloc (abfd,
203 (info->dynsym_count
204 * EXTERNAL_NLIST_SIZE)));
205 info->dynstr = (char *) bfd_alloc (abfd, info->dyninfo.ld_symb_size);
206 if (bfd_seek (abfd, info->dyninfo.ld_stab, SEEK_SET) != 0
207 || (bfd_read ((PTR) info->dynsym, info->dynsym_count,
208 EXTERNAL_NLIST_SIZE, abfd)
209 != info->dynsym_count * EXTERNAL_NLIST_SIZE)
210 || bfd_seek (abfd, info->dyninfo.ld_symbols, SEEK_SET) != 0
211 || (bfd_read ((PTR) info->dynstr, 1, info->dyninfo.ld_symb_size,
212 abfd)
213 != info->dyninfo.ld_symb_size))
214 return (bfd_size_type) -1;
215 }
1a602d6e 216
0ee75d02
ILT
217 *syms = info->dynsym;
218 *strs = info->dynstr;
219 *strsize = info->dyninfo.ld_symb_size;
4a81b561 220
0ee75d02
ILT
221#ifdef CHECK_DYNAMIC_HASH
222 /* Check my understanding of the dynamic hash table by making sure
223 that each symbol can be located in the hash table. */
224 {
225 bfd_size_type table_size;
226 bfd_byte *table;
227 bfd_size_type i;
228
229 if (info->dyninfo.ld_buckets > info->dynsym_count)
230 abort ();
231 table_size = info->dyninfo.ld_stab - info->dyninfo.ld_hash;
232 table = (bfd_byte *) alloca (table_size);
233 if (bfd_seek (abfd, info->dyninfo.ld_hash, SEEK_SET) != 0
234 || bfd_read ((PTR) table, 1, table_size, abfd) != table_size)
235 abort ();
236 for (i = 0; i < info->dynsym_count; i++)
9846338e 237 {
0ee75d02
ILT
238 unsigned char *name;
239 unsigned long hash;
240
241 name = ((unsigned char *) info->dynstr
242 + GET_WORD (abfd, info->dynsym[i].e_strx));
243 hash = 0;
244 while (*name != '\0')
245 hash = (hash << 1) + *name++;
246 hash &= 0x7fffffff;
247 hash %= info->dyninfo.ld_buckets;
248 while (GET_WORD (abfd, table + 8 * hash) != i)
249 {
250 hash = GET_WORD (abfd, table + 8 * hash + 4);
251 if (hash == 0 || hash >= table_size / 8)
252 abort ();
253 }
9846338e 254 }
4a81b561 255 }
0ee75d02 256#endif /* CHECK_DYNAMIC_HASH */
4a81b561 257
0ee75d02 258 return info->dynsym_count;
4a81b561 259}
4a81b561 260
0ee75d02 261/* Read in the dynamic relocs for a section. */
4a81b561 262
0ee75d02
ILT
263static bfd_size_type
264MY(read_dynamic_relocs) (abfd, relocs)
4a81b561 265 bfd *abfd;
0ee75d02 266 PTR *relocs;
4a81b561 267{
0ee75d02 268 struct sunos_dynamic_info *info;
4a81b561 269
0ee75d02
ILT
270 if (obj_aout_dynamic_info (abfd) == (PTR) NULL)
271 {
272 if (! sunos_read_dynamic_info (abfd))
273 return (bfd_size_type) -1;
274 }
4a81b561 275
0ee75d02
ILT
276 info = (struct sunos_dynamic_info *) obj_aout_dynamic_info (abfd);
277 if (! info->valid || info->dynrel_count == 0)
4a81b561 278 return 0;
4a81b561 279
0ee75d02
ILT
280 if (info->dynrel == (struct external_nlist *) NULL)
281 {
282 info->dynrel = (PTR) bfd_alloc (abfd,
283 (info->dynrel_count
284 * obj_reloc_entry_size (abfd)));
285 if (bfd_seek (abfd, info->dyninfo.ld_rel, SEEK_SET) != 0
286 || (bfd_read ((PTR) info->dynrel, info->dynrel_count,
287 obj_reloc_entry_size (abfd), abfd)
288 != info->dynrel_count * obj_reloc_entry_size (abfd)))
289 return (bfd_size_type) -1;
290 }
4a81b561 291
0ee75d02 292 *relocs = info->dynrel;
4a81b561 293
0ee75d02 294 return info->dynrel_count;
4a81b561 295}