static file_ptr memory_bwrite (bfd *abfd, const void *ptr, file_ptr size) { struct bfd_in_memory *bim = (struct bfd_in_memory *) abfd->iostream; if (abfd->where + size > bim->size) { bfd_size_type newsize, oldsize; oldsize = (bim->size + 127) & ~(bfd_size_type) 127; bim->size = abfd->where + size; /* Round up to cut down on memory fragmentation */ newsize = (bim->size + 127) & ~(bfd_size_type) 127; if (newsize > oldsize) { bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize); if (bim->buffer == NULL) { bim->size = 0; return 0; } if (newsize > bim->size) memset (bim->buffer + bim->size, 0, newsize - bim->size); } } memcpy (bim->buffer + abfd->where, ptr, (size_t) size); return size; }
static int memory_bseek (bfd *abfd, file_ptr position, int direction) { file_ptr nwhere; struct bfd_in_memory *bim; bim = (struct bfd_in_memory *) abfd->iostream; if (direction == SEEK_SET) nwhere = position; else nwhere = abfd->where + position; if (nwhere < 0) { abfd->where = 0; errno = EINVAL; return -1; } if ((bfd_size_type)nwhere > bim->size) { if (abfd->direction == write_direction || abfd->direction == both_direction) { bfd_size_type newsize, oldsize; oldsize = (bim->size + 127) & ~(bfd_size_type) 127; bim->size = nwhere; /* Round up to cut down on memory fragmentation */ newsize = (bim->size + 127) & ~(bfd_size_type) 127; if (newsize > oldsize) { bim->buffer = (bfd_byte *) bfd_realloc_or_free (bim->buffer, newsize); if (bim->buffer == NULL) { errno = EINVAL; bim->size = 0; return -1; } memset (bim->buffer + oldsize, 0, newsize - oldsize); } } else { abfd->where = bim->size; errno = EINVAL; bfd_set_error (bfd_error_file_truncated); return -1; } } return 0; }
bfd_size_type bfd_bwrite (const void *ptr, bfd_size_type size, bfd *abfd) { size_t nwrote; if ((abfd->flags & BFD_IN_MEMORY) != 0) { struct bfd_in_memory *bim = abfd->iostream; size = (size_t) size; if (abfd->where + size > bim->size) { bfd_size_type newsize, oldsize; oldsize = (bim->size + 127) & ~(bfd_size_type) 127; bim->size = abfd->where + size; /* Round up to cut down on memory fragmentation */ newsize = (bim->size + 127) & ~(bfd_size_type) 127; if (newsize > oldsize) { bim->buffer = bfd_realloc_or_free (bim->buffer, newsize); if (bim->buffer == NULL) { bim->size = 0; return 0; } if (newsize > bim->size) memset (bim->buffer + bim->size, 0, newsize - bim->size); } } memcpy (bim->buffer + abfd->where, ptr, (size_t) size); abfd->where += size; return size; } if (abfd->iovec) nwrote = abfd->iovec->bwrite (abfd, ptr, size); else nwrote = 0; if (nwrote != (size_t) -1) abfd->where += nwrote; if (nwrote != size) { #ifdef ENOSPC errno = ENOSPC; #endif bfd_set_error (bfd_error_system_call); } return nwrote; }
bfd_size_type _bfd_elf_strtab_add (struct elf_strtab_hash *tab, const char *str, bfd_boolean copy) { register struct elf_strtab_hash_entry *entry; /* We handle this specially, since we don't want to do refcounting on it. */ if (*str == '\0') return 0; BFD_ASSERT (tab->sec_size == 0); entry = (struct elf_strtab_hash_entry *) bfd_hash_lookup (&tab->table, str, TRUE, copy); if (entry == NULL) return (bfd_size_type) -1; entry->refcount++; if (entry->len == 0) { entry->len = strlen (str) + 1; /* 2G strings lose. */ BFD_ASSERT (entry->len > 0); if (tab->size == tab->alloced) { bfd_size_type amt = sizeof (struct elf_strtab_hash_entry *); tab->alloced *= 2; tab->array = (struct elf_strtab_hash_entry **) bfd_realloc_or_free (tab->array, tab->alloced * amt); if (tab->array == NULL) return (bfd_size_type) -1; } entry->u.index = tab->size++; tab->array[entry->u.index] = entry; } return entry->u.index; }
int bfd_seek (bfd *abfd, file_ptr position, int direction) { int result; file_ptr file_position; /* For the time being, a BFD may not seek to it's end. The problem is that we don't easily have a way to recognize the end of an element in an archive. */ BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR); if (direction == SEEK_CUR && position == 0) return 0; if ((abfd->flags & BFD_IN_MEMORY) != 0) { struct bfd_in_memory *bim; bim = abfd->iostream; if (direction == SEEK_SET) abfd->where = position; else abfd->where += position; if (abfd->where > bim->size) { if ((abfd->direction == write_direction) || (abfd->direction == both_direction)) { bfd_size_type newsize, oldsize; oldsize = (bim->size + 127) & ~(bfd_size_type) 127; bim->size = abfd->where; /* Round up to cut down on memory fragmentation */ newsize = (bim->size + 127) & ~(bfd_size_type) 127; if (newsize > oldsize) { bim->buffer = bfd_realloc_or_free (bim->buffer, newsize); if (bim->buffer == NULL) { bim->size = 0; return -1; } } } else { abfd->where = bim->size; bfd_set_error (bfd_error_file_truncated); return -1; } } return 0; } if (abfd->format != bfd_archive && abfd->my_archive == 0) { if (direction == SEEK_SET && (bfd_vma) position == abfd->where) return 0; } else { /* We need something smarter to optimize access to archives. Currently, anything inside an archive is read via the file handle for the archive. Which means that a bfd_seek on one component affects the `current position' in the archive, as well as in any other component. It might be sufficient to put a spike through the cache abstraction, and look to the archive for the file position, but I think we should try for something cleaner. In the meantime, no optimization for archives. */ } file_position = position; if (direction == SEEK_SET && abfd->my_archive != NULL) file_position += abfd->origin; if (abfd->iovec) result = abfd->iovec->bseek (abfd, file_position, direction); else result = -1; if (result != 0) { int hold_errno = errno; /* Force redetermination of `where' field. */ bfd_tell (abfd); /* An EINVAL error probably means that the file offset was absurd. */ if (hold_errno == EINVAL) bfd_set_error (bfd_error_file_truncated); else { bfd_set_error (bfd_error_system_call); errno = hold_errno; } } else { /* Adjust `where' field. */ if (direction == SEEK_SET) abfd->where = position; else abfd->where += position; } return result; }
bfd_boolean bfd_get_file_window (bfd *abfd, file_ptr offset, bfd_size_type size, bfd_window *windowp, bfd_boolean writable) { static size_t pagesize; bfd_window_internal *i = windowp->i; bfd_size_type size_to_alloc = size; if (debug_windows) fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)", abfd, (long) offset, (long) size, windowp, windowp->data, (unsigned long) windowp->size, windowp->i, writable); /* Make sure we know the page size, so we can be friendly to mmap. */ if (pagesize == 0) pagesize = getpagesize (); if (pagesize == 0) abort (); if (i == NULL) { i = bfd_zmalloc (sizeof (bfd_window_internal)); if (i == NULL) return FALSE; i->data = NULL; } #ifdef HAVE_MMAP if (ok_to_map && (i->data == NULL || i->mapped == 1) && (abfd->flags & BFD_IN_MEMORY) == 0) { file_ptr file_offset, offset2; size_t real_size; int fd; /* Find the real file and the real offset into it. */ while (abfd->my_archive != NULL && !bfd_is_thin_archive (abfd->my_archive)) { offset += abfd->origin; abfd = abfd->my_archive; } /* Seek into the file, to ensure it is open if cacheable. */ if (abfd->iostream == NULL && (abfd->iovec == NULL || abfd->iovec->bseek (abfd, offset, SEEK_SET) != 0)) goto free_and_fail; fd = fileno ((FILE *) abfd->iostream); /* Compute offsets and size for mmap and for the user's data. */ offset2 = offset % pagesize; if (offset2 < 0) abort (); file_offset = offset - offset2; real_size = offset + size - file_offset; real_size = real_size + pagesize - 1; real_size -= real_size % pagesize; /* If we're re-using a memory region, make sure it's big enough. */ if (i->data != NULL && i->size < size) { munmap (i->data, i->size); i->data = NULL; } i->data = mmap (i->data, real_size, writable ? PROT_WRITE | PROT_READ : PROT_READ, (writable ? MAP_FILE | MAP_PRIVATE : MAP_FILE | MAP_SHARED), fd, file_offset); if (i->data == (void *) -1) { /* An error happened. Report it, or try using malloc, or something. */ bfd_set_error (bfd_error_system_call); windowp->data = 0; if (debug_windows) fprintf (stderr, "\t\tmmap failed!\n"); goto free_and_fail; } if (debug_windows) fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n", (long) real_size, i->data, (long) offset2); i->size = real_size; windowp->data = (bfd_byte *) i->data + offset2; windowp->size = size; i->mapped = 1; i->refcount = 1; windowp->i = i; return TRUE; } else if (debug_windows) { if (ok_to_map) fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"), (unsigned long) i->data, (int) i->mapped); else fprintf (stderr, _("not mapping: env var not set\n")); } #else ok_to_map = 0; #endif #ifdef HAVE_MPROTECT if (!writable) { size_to_alloc += pagesize - 1; size_to_alloc -= size_to_alloc % pagesize; } #endif if (debug_windows) fprintf (stderr, "\n\t%s(%6ld)", i->data ? "realloc" : " malloc", (long) size_to_alloc); i->data = bfd_realloc_or_free (i->data, size_to_alloc); if (debug_windows) fprintf (stderr, "\t-> %p\n", i->data); if (i->data == NULL) { if (size_to_alloc == 0) { windowp->i = i; return TRUE; } goto free_and_fail; } i->refcount = 1; if (bfd_seek (abfd, offset, SEEK_SET) != 0) goto free_and_fail; i->size = bfd_bread (i->data, size, abfd); if (i->size != size) goto free_and_fail; i->mapped = 0; #ifdef HAVE_MPROTECT if (!writable) { if (debug_windows) fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data, (long) i->size); mprotect (i->data, i->size, PROT_READ); } #endif windowp->data = i->data; windowp->size = i->size; windowp->i = i; return TRUE; free_and_fail: /* We have a bfd_window_internal, but an error occurred. Free it. */ free (i); return FALSE; }
bfd_boolean _bfd_link_section_stabs (bfd *abfd, struct stab_info *sinfo, asection *stabsec, asection *stabstrsec, void * *psecinfo, bfd_size_type *pstring_offset) { bfd_boolean first; bfd_size_type count, amt; struct stab_section_info *secinfo; bfd_byte *stabbuf = NULL; bfd_byte *stabstrbuf = NULL; bfd_byte *sym, *symend; bfd_size_type stroff, next_stroff, skip; bfd_size_type *pstridx; if (stabsec->size == 0 || stabstrsec->size == 0) /* This file does not contain stabs debugging information. */ return TRUE; if (stabsec->size % STABSIZE != 0) /* Something is wrong with the format of these stab symbols. Don't try to optimize them. */ return TRUE; if ((stabstrsec->flags & SEC_RELOC) != 0) /* We shouldn't see relocations in the strings, and we aren't prepared to handle them. */ return TRUE; if (bfd_is_abs_section (stabsec->output_section) || bfd_is_abs_section (stabstrsec->output_section)) /* At least one of the sections is being discarded from the link, so we should just ignore them. */ return TRUE; first = FALSE; if (sinfo->stabstr == NULL) { flagword flags; /* Initialize the stabs information we need to keep track of. */ first = TRUE; sinfo->strings = _bfd_stringtab_init (); if (sinfo->strings == NULL) goto error_return; /* Make sure the first byte is zero. */ (void) _bfd_stringtab_add (sinfo->strings, "", TRUE, TRUE); if (! bfd_hash_table_init (&sinfo->includes, stab_link_includes_newfunc, sizeof (struct stab_link_includes_entry))) goto error_return; flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING | SEC_LINKER_CREATED); sinfo->stabstr = bfd_make_section_anyway_with_flags (abfd, ".stabstr", flags); if (sinfo->stabstr == NULL) goto error_return; } /* Initialize the information we are going to store for this .stab section. */ count = stabsec->size / STABSIZE; amt = sizeof (struct stab_section_info); amt += (count - 1) * sizeof (bfd_size_type); *psecinfo = bfd_alloc (abfd, amt); if (*psecinfo == NULL) goto error_return; secinfo = (struct stab_section_info *) *psecinfo; secinfo->excls = NULL; stabsec->rawsize = stabsec->size; secinfo->cumulative_skips = NULL; memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type)); /* Read the stabs information from abfd. */ if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf) || !bfd_malloc_and_get_section (abfd, stabstrsec, &stabstrbuf)) goto error_return; /* Look through the stabs symbols, work out the new string indices, and identify N_BINCL symbols which can be eliminated. */ stroff = 0; /* The stabs sections can be split when -split-by-reloc/-split-by-file is used. We must keep track of each stab section's place in the single concatenated string table. */ next_stroff = pstring_offset ? *pstring_offset : 0; skip = 0; symend = stabbuf + stabsec->size; for (sym = stabbuf, pstridx = secinfo->stridxs; sym < symend; sym += STABSIZE, ++pstridx) { bfd_size_type symstroff; int type; const char *string; if (*pstridx != 0) /* This symbol has already been handled by an N_BINCL pass. */ continue; type = sym[TYPEOFF]; if (type == 0) { /* Special type 0 stabs indicate the offset to the next string table. We only copy the very first one. */ stroff = next_stroff; next_stroff += bfd_get_32 (abfd, sym + 8); if (pstring_offset) *pstring_offset = next_stroff; if (! first) { *pstridx = (bfd_size_type) -1; ++skip; continue; } first = FALSE; } /* Store the string in the hash table, and record the index. */ symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF); if (symstroff >= stabstrsec->size) { _bfd_error_handler /* xgettext:c-format */ (_("%B(%A+0x%lx): Stabs entry has invalid string index."), abfd, stabsec, (long) (sym - stabbuf)); bfd_set_error (bfd_error_bad_value); goto error_return; } string = (char *) stabstrbuf + symstroff; *pstridx = _bfd_stringtab_add (sinfo->strings, string, TRUE, TRUE); /* An N_BINCL symbol indicates the start of the stabs entries for a header file. We need to scan ahead to the next N_EINCL symbol, ignoring nesting, adding up all the characters in the symbol names, not including the file numbers in types (the first number after an open parenthesis). */ if (type == (int) N_BINCL) { bfd_vma sum_chars; bfd_vma num_chars; bfd_vma buf_len = 0; char * symb; char * symb_rover; int nest; bfd_byte * incl_sym; struct stab_link_includes_entry * incl_entry; struct stab_link_includes_totals * t; struct stab_excl_list * ne; symb = symb_rover = NULL; sum_chars = num_chars = 0; nest = 0; for (incl_sym = sym + STABSIZE; incl_sym < symend; incl_sym += STABSIZE) { int incl_type; incl_type = incl_sym[TYPEOFF]; if (incl_type == 0) break; else if (incl_type == (int) N_EXCL) continue; else if (incl_type == (int) N_EINCL) { if (nest == 0) break; --nest; } else if (incl_type == (int) N_BINCL) ++nest; else if (nest == 0) { const char *str; str = ((char *) stabstrbuf + stroff + bfd_get_32 (abfd, incl_sym + STRDXOFF)); for (; *str != '\0'; str++) { if (num_chars >= buf_len) { buf_len += 32 * 1024; symb = (char *) bfd_realloc_or_free (symb, buf_len); if (symb == NULL) goto error_return; symb_rover = symb + num_chars; } * symb_rover ++ = * str; sum_chars += *str; num_chars ++; if (*str == '(') { /* Skip the file number. */ ++str; while (ISDIGIT (*str)) ++str; --str; } } } } BFD_ASSERT (num_chars == (bfd_vma) (symb_rover - symb)); /* If we have already included a header file with the same value, then replaced this one with an N_EXCL symbol. */ incl_entry = (struct stab_link_includes_entry * ) bfd_hash_lookup (&sinfo->includes, string, TRUE, TRUE); if (incl_entry == NULL) goto error_return; for (t = incl_entry->totals; t != NULL; t = t->next) if (t->sum_chars == sum_chars && t->num_chars == num_chars && memcmp (t->symb, symb, num_chars) == 0) break; /* Record this symbol, so that we can set the value correctly. */ amt = sizeof *ne; ne = (struct stab_excl_list *) bfd_alloc (abfd, amt); if (ne == NULL) goto error_return; ne->offset = sym - stabbuf; ne->val = sum_chars; ne->type = (int) N_BINCL; ne->next = secinfo->excls; secinfo->excls = ne; if (t == NULL) { /* This is the first time we have seen this header file with this set of stabs strings. */ t = (struct stab_link_includes_totals *) bfd_hash_allocate (&sinfo->includes, sizeof *t); if (t == NULL) goto error_return; t->sum_chars = sum_chars; t->num_chars = num_chars; /* Trim data down. */ t->symb = symb = (char *) bfd_realloc_or_free (symb, num_chars); t->next = incl_entry->totals; incl_entry->totals = t; } else { bfd_size_type *incl_pstridx; /* We have seen this header file before. Tell the final pass to change the type to N_EXCL. */ ne->type = (int) N_EXCL; /* Free off superfluous symbols. */ free (symb); /* Mark the skipped symbols. */ nest = 0; for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1; incl_sym < symend; incl_sym += STABSIZE, ++incl_pstridx) { int incl_type; incl_type = incl_sym[TYPEOFF]; if (incl_type == (int) N_EINCL) { if (nest == 0) { *incl_pstridx = (bfd_size_type) -1; ++skip; break; } --nest; } else if (incl_type == (int) N_BINCL) ++nest; else if (incl_type == (int) N_EXCL) /* Keep existing exclusion marks. */ continue; else if (nest == 0) { *incl_pstridx = (bfd_size_type) -1; ++skip; } } } } } free (stabbuf); stabbuf = NULL; free (stabstrbuf); stabstrbuf = NULL; /* We need to set the section sizes such that the linker will compute the output section sizes correctly. We set the .stab size to not include the entries we don't want. We set SEC_EXCLUDE for the .stabstr section, so that it will be dropped from the link. We record the size of the strtab in the first .stabstr section we saw, and make sure we don't set SEC_EXCLUDE for that section. */ stabsec->size = (count - skip) * STABSIZE; if (stabsec->size == 0) stabsec->flags |= SEC_EXCLUDE | SEC_KEEP; stabstrsec->flags |= SEC_EXCLUDE | SEC_KEEP; sinfo->stabstr->size = _bfd_stringtab_size (sinfo->strings); /* Calculate the `cumulative_skips' array now that stabs have been deleted for this section. */ if (skip != 0) { bfd_size_type i, offset; bfd_size_type *pskips; amt = count * sizeof (bfd_size_type); secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt); if (secinfo->cumulative_skips == NULL) goto error_return; pskips = secinfo->cumulative_skips; pstridx = secinfo->stridxs; offset = 0; for (i = 0; i < count; i++, pskips++, pstridx++) { *pskips = offset; if (*pstridx == (bfd_size_type) -1) offset += STABSIZE; } BFD_ASSERT (offset != 0); } return TRUE; error_return: if (stabbuf != NULL) free (stabbuf); if (stabstrbuf != NULL) free (stabstrbuf); return FALSE; }