Commit | Line | Data |
---|---|---|
fc723380 JG |
1 | /* opncls.c -- open and close a bfd. */ |
2 | ||
4a81b561 DHW |
3 | /* Copyright (C) 1990, 1991 Free Software Foundation, Inc. |
4 | ||
5 | This file is part of BFD, the Binary File Diddler. | |
6 | ||
7 | BFD 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 1, or (at your option) | |
10 | any later version. | |
11 | ||
12 | BFD 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 BFD; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | /* $Id$ */ | |
22 | ||
4a81b561 DHW |
23 | #include "sysdep.h" |
24 | #include "bfd.h" | |
25 | #include "libbfd.h" | |
26 | ||
4a81b561 DHW |
27 | extern void bfd_cache_init(); |
28 | FILE *bfd_open_file(); | |
29 | ||
30 | /* fdopen is a loser -- we should use stdio exclusively. Unfortunately | |
31 | if we do that we can't use fcntl. */ | |
32 | \f | |
33 | /** Locking | |
34 | ||
35 | Locking is loosely controlled by the preprocessor variable | |
36 | BFD_LOCKS. I say loosely because Unix barely understands locking | |
37 | -- at least in BSD it doesn't affect programs which don't | |
38 | explicitly use it! That is to say it's practically useless, though | |
39 | if everyone uses this library you'll be OK. | |
40 | ||
41 | From among the many and varied lock facilities available, (none of | |
42 | which, of course, knows about any other) we use the fcntl locks, | |
43 | because they're Posix. | |
44 | ||
45 | The reason that bfd_openr and bfd_fdopenr exist, yet only bfd_openw | |
46 | exists is because of locking. When we do output, we lock the | |
47 | filename file for output, then open a temporary file which does not | |
48 | actually get its correct filename until closing time. This is | |
49 | safest, but requires the asymmetry in read and write entry points. | |
50 | ||
51 | Perhaps, since unix has so many different kinds of locking anyway, | |
52 | we should use the emacs lock scheme?... */ | |
4a81b561 | 53 | |
9872a49c SC |
54 | #define obstack_chunk_alloc malloc |
55 | #define obstack_chunk_free free | |
56 | ||
fc723380 JG |
57 | /* Return a new BFD. All BFD's are allocated through this routine. */ |
58 | ||
4a81b561 DHW |
59 | bfd *new_bfd() |
60 | { | |
9872a49c SC |
61 | struct obstack tmp; |
62 | bfd *nbfd; | |
39a2ce33 | 63 | obstack_begin(&tmp,128); |
9872a49c SC |
64 | |
65 | nbfd = (bfd *)obstack_alloc(&tmp,sizeof(bfd)); | |
fc723380 JG |
66 | memset((PTR)nbfd, 0, sizeof (bfd)); /* Clear it */ |
67 | ||
9872a49c | 68 | nbfd->memory = tmp; |
4a81b561 DHW |
69 | |
70 | nbfd->direction = no_direction; | |
71 | nbfd->iostream = NULL; | |
72 | nbfd->where = 0; | |
73 | nbfd->sections = (asection *)NULL; | |
74 | nbfd->format = bfd_unknown; | |
75 | nbfd->my_archive = (bfd *)NULL; | |
76 | nbfd->origin = 0; | |
77 | nbfd->opened_once = false; | |
78 | nbfd->output_has_begun = false; | |
79 | nbfd->section_count = 0; | |
9846338e | 80 | nbfd->usrdata = (PTR)NULL; |
4a81b561 DHW |
81 | nbfd->sections = (asection *)NULL; |
82 | nbfd->cacheable = false; | |
83 | nbfd->flags = NO_FLAGS; | |
fc723380 | 84 | nbfd->mtime_set = 0; |
4a81b561 DHW |
85 | return nbfd; |
86 | } | |
fc723380 JG |
87 | |
88 | /* Allocate a new BFD as a member of archive OBFD. */ | |
89 | ||
4a81b561 DHW |
90 | bfd *new_bfd_contained_in(obfd) |
91 | bfd *obfd; | |
92 | { | |
9846338e | 93 | bfd *nbfd = new_bfd(); |
4a81b561 DHW |
94 | nbfd->xvec = obfd->xvec; |
95 | nbfd->my_archive = obfd; | |
96 | nbfd->direction = read_direction; | |
97 | return nbfd; | |
98 | } | |
99 | ||
100 | /** bfd_openr, bfd_fdopenr -- open for reading. | |
101 | Returns a pointer to a freshly-allocated bfd on success, or NULL. */ | |
102 | ||
103 | bfd * | |
9846338e SC |
104 | DEFUN(bfd_openr, (filename, target), |
105 | CONST char *filename AND | |
106 | CONST char *target) | |
4a81b561 DHW |
107 | { |
108 | bfd *nbfd; | |
109 | bfd_target *target_vec; | |
110 | ||
111 | target_vec = bfd_find_target (target); | |
112 | if (target_vec == NULL) { | |
113 | bfd_error = invalid_target; | |
114 | return NULL; | |
115 | } | |
116 | ||
117 | bfd_error = system_call_error; | |
118 | nbfd = new_bfd(); | |
119 | if (nbfd == NULL) { | |
120 | bfd_error = no_memory; | |
121 | return NULL; | |
122 | } | |
123 | ||
124 | nbfd->filename = filename; | |
125 | nbfd->xvec = target_vec; | |
126 | nbfd->direction = read_direction; | |
127 | ||
128 | if (bfd_open_file (nbfd) == NULL) { | |
129 | bfd_error = system_call_error; /* File didn't exist, or some such */ | |
9872a49c | 130 | bfd_release(nbfd,0); |
4a81b561 DHW |
131 | return NULL; |
132 | } | |
133 | return nbfd; | |
134 | } | |
135 | ||
136 | ||
137 | /* Don't try to `optimize' this function: | |
138 | ||
139 | o - We lock using stack space so that interrupting the locking | |
140 | won't cause a storage leak. | |
141 | o - We open the file stream last, since we don't want to have to | |
142 | close it if anything goes wrong. Closing the stream means closing | |
143 | the file descriptor too, even though we didn't open it. | |
144 | */ | |
145 | ||
146 | bfd * | |
9846338e SC |
147 | DEFUN(bfd_fdopenr,(filename, target, fd), |
148 | CONST char *filename AND | |
149 | CONST char *target AND | |
150 | int fd) | |
4a81b561 DHW |
151 | { |
152 | bfd *nbfd; | |
153 | bfd_target *target_vec; | |
154 | int fdflags; | |
155 | #ifdef BFD_LOCKS | |
156 | struct flock lock, *lockp = &lock; | |
157 | #endif | |
158 | ||
159 | target_vec = bfd_find_target (target); | |
160 | if (target_vec == NULL) { | |
161 | bfd_error = invalid_target; | |
162 | return NULL; | |
163 | } | |
164 | ||
165 | bfd_error = system_call_error; | |
166 | ||
167 | fdflags = fcntl (fd, F_GETFL); | |
168 | if (fdflags == -1) return NULL; | |
169 | ||
170 | #ifdef BFD_LOCKS | |
171 | lockp->l_type = F_RDLCK; | |
172 | if (fcntl (fd, F_SETLKW, lockp) == -1) return NULL; | |
173 | #endif | |
174 | ||
175 | nbfd = new_bfd(); | |
176 | ||
177 | if (nbfd == NULL) { | |
178 | bfd_error = no_memory; | |
179 | return NULL; | |
180 | } | |
181 | #ifdef BFD_LOCKS | |
182 | nbfd->lock = (struct flock *) (nbfd + 1); | |
183 | #endif | |
184 | /* if the fd were open for read only, this still would not hurt: */ | |
185 | nbfd->iostream = (char *) fdopen (fd, "r+"); | |
186 | if (nbfd->iostream == NULL) { | |
fc723380 | 187 | (void) obstack_free (&nbfd->memory, (PTR)0); |
4a81b561 DHW |
188 | return NULL; |
189 | } | |
190 | ||
191 | /* OK, put everything where it belongs */ | |
192 | ||
193 | nbfd->filename = filename; | |
194 | nbfd->xvec = target_vec; | |
195 | ||
196 | /* As a special case we allow a FD open for read/write to | |
197 | be written through, although doing so requires that we end | |
198 | the previous clause with a preposition. */ | |
199 | switch (fdflags & O_ACCMODE) { | |
200 | case O_RDONLY: nbfd->direction = read_direction; break; | |
201 | case O_WRONLY: nbfd->direction = write_direction; break; | |
202 | case O_RDWR: nbfd->direction = both_direction; break; | |
203 | default: abort (); | |
204 | } | |
205 | ||
206 | #ifdef BFD_LOCKS | |
207 | memcpy (nbfd->lock, lockp, sizeof (struct flock)) | |
208 | #endif | |
209 | ||
210 | bfd_cache_init (nbfd); | |
211 | ||
212 | return nbfd; | |
213 | } | |
214 | \f | |
215 | /** bfd_openw -- open for writing. | |
216 | Returns a pointer to a freshly-allocated bfd on success, or NULL. | |
217 | ||
218 | See comment by bfd_fdopenr before you try to modify this function. */ | |
219 | ||
220 | bfd * | |
9846338e SC |
221 | DEFUN(bfd_openw,(filename, target), |
222 | CONST char *filename AND | |
223 | CONST char *target) | |
4a81b561 DHW |
224 | { |
225 | bfd *nbfd; | |
226 | bfd_target *target_vec; | |
227 | ||
228 | target_vec = bfd_find_target (target); | |
229 | if (target_vec == NULL) return NULL; | |
230 | ||
231 | bfd_error = system_call_error; | |
232 | ||
233 | /* nbfd has to point to head of malloc'ed block so that bfd_close may | |
234 | reclaim it correctly. */ | |
235 | ||
236 | nbfd = new_bfd(); | |
237 | if (nbfd == NULL) { | |
238 | bfd_error = no_memory; | |
239 | return NULL; | |
240 | } | |
241 | ||
242 | nbfd->filename = filename; | |
243 | nbfd->xvec = target_vec; | |
244 | nbfd->direction = write_direction; | |
245 | ||
246 | if (bfd_open_file (nbfd) == NULL) { | |
247 | bfd_error = system_call_error; /* File not writeable, etc */ | |
fc723380 | 248 | (void) obstack_free (&nbfd->memory, (PTR)0); |
4a81b561 DHW |
249 | return NULL; |
250 | } | |
251 | return nbfd; | |
252 | } | |
253 | ||
254 | ||
255 | \f | |
256 | /** Close up shop, get your deposit back. */ | |
257 | boolean | |
258 | bfd_close (abfd) | |
259 | bfd *abfd; | |
260 | { | |
261 | if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false; | |
262 | ||
263 | bfd_cache_close(abfd); | |
264 | /* If the file was open for writing and is now executable | |
265 | make it so */ | |
266 | if (abfd->direction == write_direction | |
267 | && abfd->flags & EXEC_P) { | |
268 | struct stat buf; | |
269 | stat(abfd->filename, &buf); | |
270 | chmod(abfd->filename,buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH); | |
271 | } | |
fc723380 | 272 | (void) obstack_free (&abfd->memory, (PTR)0); |
4a81b561 DHW |
273 | return true; |
274 | } | |
fc723380 JG |
275 | |
276 | /* Create a bfd with no associated file or target. */ | |
277 | ||
4a81b561 | 278 | bfd * |
9846338e SC |
279 | DEFUN(bfd_create,(filename, template), |
280 | CONST char *filename AND | |
281 | CONST bfd *template) | |
4a81b561 DHW |
282 | { |
283 | bfd *nbfd = new_bfd(); | |
284 | if (nbfd == (bfd *)NULL) { | |
285 | bfd_error = no_memory; | |
286 | return (bfd *)NULL; | |
287 | } | |
288 | nbfd->filename = filename; | |
9872a49c SC |
289 | if(template) { |
290 | nbfd->xvec = template->xvec; | |
291 | } | |
4a81b561 | 292 | nbfd->direction = no_direction; |
9872a49c | 293 | bfd_set_format(nbfd, bfd_object); |
4a81b561 | 294 | return nbfd; |
4a81b561 | 295 | } |
9872a49c | 296 | |
fc723380 JG |
297 | /* Memory allocation */ |
298 | ||
9872a49c SC |
299 | DEFUN(PTR bfd_alloc, (abfd, size), |
300 | bfd *abfd AND | |
fc723380 | 301 | bfd_size_type size) |
9872a49c | 302 | { |
fc723380 | 303 | PTR res = obstack_alloc(&(abfd->memory), (int)size); |
9872a49c SC |
304 | return res; |
305 | } | |
306 | ||
307 | DEFUN(PTR bfd_zalloc,(abfd, size), | |
308 | bfd *abfd AND | |
fc723380 | 309 | bfd_size_type size) |
9872a49c SC |
310 | { |
311 | PTR res = bfd_alloc(abfd, size); | |
fc723380 | 312 | memset(res, 0, (size_t)size); |
9872a49c SC |
313 | return res; |
314 | } | |
315 | ||
316 | DEFUN(PTR bfd_realloc,(abfd, old, size), | |
317 | bfd *abfd AND | |
318 | PTR old AND | |
fc723380 | 319 | bfd_size_type size) |
9872a49c SC |
320 | { |
321 | PTR res = bfd_alloc(abfd, size); | |
fc723380 | 322 | memcpy(res, old, (size_t)size); |
9872a49c SC |
323 | return res; |
324 | } | |
325 | ||
326 | ||
fc723380 | 327 | DEFUN(bfd_size_type bfd_alloc_size,(abfd), |
9872a49c SC |
328 | bfd *abfd) |
329 | { | |
330 | struct _obstack_chunk *chunk = abfd->memory.chunk; | |
331 | size_t size = 0; | |
332 | while (chunk) { | |
333 | size += chunk->limit - &(chunk->contents[0]); | |
334 | chunk = chunk->prev; | |
335 | } | |
336 | return size; | |
337 | } |