2.14 Opening and closing BFDs

2.14.1 Functions for opening and closing

2.14.1.1 _bfd_new_bfd

Function: bfd *_bfd_new_bfd (void);

Return a new BFD. All BFD’s are allocated through this routine.

2.14.1.2 _bfd_new_bfd_contained_in

Function: bfd *_bfd_new_bfd_contained_in (bfd *);

Allocate a new BFD as a member of archive OBFD.

2.14.1.3 _bfd_free_cached_info

Function: bool _bfd_free_cached_info (bfd *);

Free objalloc memory.

2.14.1.4 bfd_fopen

Function: bfd *bfd_fopen (const char *filename, const char *target, const char *mode, int fd);

Open the file filename with the target target. Return a pointer to the created BFD. If fd is not -1, then fdopen is used to open the file; otherwise, fopen is used. mode is passed directly to fopen or fdopen.

Calls bfd_find_target, so target is interpreted as by that function.

The new BFD is marked as cacheable iff fd is -1.

If NULL is returned then an error has occured. Possible errors are bfd_error_no_memory, bfd_error_invalid_target or system_call error.

On error, fd is always closed.

A copy of the filename argument is stored in the newly created BFD. It can be accessed via the bfd_get_filename() macro.

2.14.1.5 bfd_openr

Function: bfd *bfd_openr (const char *filename, const char *target);

Open the file filename (using fopen) with the target target. Return a pointer to the created BFD.

Calls bfd_find_target, so target is interpreted as by that function.

If NULL is returned then an error has occured. Possible errors are bfd_error_no_memory, bfd_error_invalid_target or system_call error.

A copy of the filename argument is stored in the newly created BFD. It can be accessed via the bfd_get_filename() macro.

2.14.1.6 bfd_fdopenr

Function: bfd *bfd_fdopenr (const char *filename, const char *target, int fd);

bfd_fdopenr is to bfd_fopenr much like fdopen is to fopen. It opens a BFD on a file already described by the fd supplied.

When the file is later bfd_closed, the file descriptor will be closed. If the caller desires that this file descriptor be cached by BFD (opened as needed, closed as needed to free descriptors for other opens), with the supplied fd used as an initial file descriptor (but subject to closure at any time), call bfd_set_cacheable(bfd, 1) on the returned BFD. The default is to assume no caching; the file descriptor will remain open until bfd_close, and will not be affected by BFD operations on other files.

Possible errors are bfd_error_no_memory, bfd_error_invalid_target and bfd_error_system_call.

On error, fd is closed.

A copy of the filename argument is stored in the newly created BFD. It can be accessed via the bfd_get_filename() macro.

2.14.1.7 bfd_fdopenw

Function: bfd *bfd_fdopenw (const char *filename, const char *target, int fd);

bfd_fdopenw is exactly like bfd_fdopenr with the exception that the resulting BFD is suitable for output.

2.14.1.8 bfd_openstreamr

Function: bfd *bfd_openstreamr (const char * filename, const char * target, void * stream);

Open a BFD for read access on an existing stdio stream. When the BFD is passed to bfd_close, the stream will be closed.

A copy of the filename argument is stored in the newly created BFD. It can be accessed via the bfd_get_filename() macro.

2.14.1.9 bfd_openr_iovec

Function: bfd *bfd_openr_iovec (const char *filename, const char *target, void *(*open_func) (struct bfd *nbfd, void *open_closure), void *open_closure, file_ptr (*pread_func) (struct bfd *nbfd, void *stream, void *buf, file_ptr nbytes, file_ptr offset), int (*close_func) (struct bfd *nbfd, void *stream), int (*stat_func) (struct bfd *abfd, void *stream, struct stat *sb));

Create and return a BFD backed by a read-only stream. The stream is created using open_func, accessed using pread_func and destroyed using close_func.

Calls bfd_find_target, so target is interpreted as by that function.

Calls open_func (which can call bfd_zalloc and bfd_get_filename) to obtain the read-only stream backing the BFD. open_func either succeeds returning the non-NULL stream, or fails returning NULL (setting bfd_error).

Calls pread_func to request nbytes of data from stream starting at offset (e.g., via a call to bfd_read). pread_func either succeeds returning the number of bytes read (which can be less than nbytes when end-of-file), or fails returning -1 (setting bfd_error).

Calls close_func when the BFD is later closed using bfd_close. close_func either succeeds returning 0, or fails returning -1 (setting bfd_error).

Calls stat_func to fill in a stat structure for bfd_stat, bfd_get_size, and bfd_get_mtime calls. stat_func returns 0 on success, or returns -1 on failure (setting bfd_error).

If bfd_openr_iovec returns NULL then an error has occurred. Possible errors are bfd_error_no_memory, bfd_error_invalid_target and bfd_error_system_call.

A copy of the filename argument is stored in the newly created BFD. It can be accessed via the bfd_get_filename() macro.

2.14.1.10 bfd_openw

Function: bfd *bfd_openw (const char *filename, const char *target);

Create a BFD, associated with file filename, using the file format target, and return a pointer to it.

Possible errors are bfd_error_system_call, bfd_error_no_memory, bfd_error_invalid_target.

A copy of the filename argument is stored in the newly created BFD. It can be accessed via the bfd_get_filename() macro.

2.14.1.11 bfd_elf_bfd_from_remote_memory

Function: bfd *bfd_elf_bfd_from_remote_memory (bfd *templ, bfd_vma ehdr_vma, bfd_size_type size, bfd_vma *loadbasep, int (*target_read_memory) (bfd_vma vma, bfd_byte *myaddr, bfd_size_type len));

Create a new BFD as if by bfd_openr. Rather than opening a file, reconstruct an ELF file by reading the segments out of remote memory based on the ELF file header at EHDR_VMA and the ELF program headers it points to. If non-zero, SIZE is the known extent of the object. If not null, *LOADBASEP is filled in with the difference between the VMAs from which the segments were read, and the VMAs the file headers (and hence BFD’s idea of each section’s VMA) put them at.

The function TARGET_READ_MEMORY is called to copy LEN bytes from the remote memory at target address VMA into the local buffer at MYADDR; it should return zero on success or an errno code on failure. TEMPL must be a BFD for an ELF target with the word size and byte order found in the remote memory.

2.14.1.12 bfd_close

Function: bool bfd_close (bfd *abfd);

Close a BFD. If the BFD was open for writing, then pending operations are completed and the file written out and closed. If the created file is executable, then chmod is called to mark it as such.

All memory attached to the BFD is released.

The file descriptor associated with the BFD is closed (even if it was passed in to BFD by bfd_fdopenr).

TRUE is returned if all is ok, otherwise FALSE.

2.14.1.13 bfd_close_all_done

Function: bool bfd_close_all_done (bfd *);

Close a BFD. Differs from bfd_close since it does not complete any pending operations. This routine would be used if the application had just used BFD for swapping and didn’t want to use any of the writing code.

If the created file is executable, then chmod is called to mark it as such.

All memory attached to the BFD is released.

TRUE is returned if all is ok, otherwise FALSE.

2.14.1.14 bfd_create

Function: bfd *bfd_create (const char *filename, bfd *templ);

Create a new BFD in the manner of bfd_openw, but without opening a file. The new BFD takes the target from the target used by templ. The format is always set to bfd_object.

A copy of the filename argument is stored in the newly created BFD. It can be accessed via the bfd_get_filename() macro.

2.14.1.15 bfd_make_writable

Function: bool bfd_make_writable (bfd *abfd);

Takes a BFD as created by bfd_create and converts it into one like as returned by bfd_openw. It does this by converting the BFD to BFD_IN_MEMORY. It’s assumed that you will call bfd_make_readable on this bfd later.

TRUE is returned if all is ok, otherwise FALSE.

2.14.1.16 bfd_make_readable

Function: bool bfd_make_readable (bfd *abfd);

Takes a BFD as created by bfd_create and bfd_make_writable and converts it into one like as returned by bfd_openr. It does this by writing the contents out to the memory buffer, then reversing the direction.

TRUE is returned if all is ok, otherwise FALSE.

2.14.1.25 bfd_set_filename

Function: const char *bfd_set_filename (bfd *abfd, const char *filename);

Set the filename of abfd, copying the FILENAME parameter to bfd_alloc’d memory owned by abfd. Returns a pointer the newly allocated name, or NULL if the allocation failed.