- 论坛徽章:
- 0
|
跟踪了一下strip,发现确实修改了vaddr,但是不知道所以然,谁能解释一下最后一段?
static bfd_boolean
copy_elf_program_header (bfd *ibfd, bfd *obfd)
{
Elf_Internal_Ehdr *iehdr;
struct elf_segment_map *map;
struct elf_segment_map *map_first;
struct elf_segment_map **pointer_to_map;
Elf_Internal_Phdr *segment;
unsigned int i;
unsigned int num_segments;
bfd_boolean phdr_included = FALSE;
bfd_boolean p_paddr_valid;
iehdr = elf_elfheader (ibfd);
map_first = NULL;
pointer_to_map = &map_first;
/* If all the segment p_paddr fields are zero, don't set
map->p_paddr_valid. */
p_paddr_valid = FALSE;
num_segments = elf_elfheader (ibfd)->e_phnum;
for (i = 0, segment = elf_tdata (ibfd)->phdr;
i < num_segments;
i++, segment++)
if (segment->p_paddr != 0)
{
p_paddr_valid = TRUE;
break;
}
for (i = 0, segment = elf_tdata (ibfd)->phdr;
i < num_segments;
i++, segment++)
{
asection *section;
unsigned int section_count;
bfd_size_type amt;
Elf_Internal_Shdr *this_hdr;
asection *first_section = NULL;
asection *lowest_section = NULL;
/* Compute how many sections are in this segment. */
for (section = ibfd->sections, section_count = 0;
section != NULL;
section = section->next)
{
this_hdr = &(elf_section_data(section)->this_hdr);
if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment))
{
if (!first_section)
first_section = lowest_section = section;
if (section->lma < lowest_section->lma)
lowest_section = section;
section_count++;
}
}
/* Allocate a segment map big enough to contain
all of the sections we have selected. */
amt = sizeof (struct elf_segment_map);
if (section_count != 0)
amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
map = bfd_zalloc (obfd, amt);
if (map == NULL)
return FALSE;
/* Initialize the fields of the output segment map with the
input segment. */
map->next = NULL;
map->p_type = segment->p_type;
map->p_flags = segment->p_flags;
map->p_flags_valid = 1;
map->p_paddr = segment->p_paddr;
map->p_paddr_valid = p_paddr_valid;
map->p_align = segment->p_align;
map->p_align_valid = 1;
map->p_vaddr_offset = 0;
if (map->p_type == PT_GNU_RELRO)
{
/* The PT_GNU_RELRO segment may contain the first a few
bytes in the .got.plt section even if the whole .got.plt
section isn't in the PT_GNU_RELRO segment. We won't
change the size of the PT_GNU_RELRO segment. */
map->p_size = segment->p_memsz;
map->p_size_valid = 1;
}
/* Determine if this segment contains the ELF file header
and if it contains the program headers themselves. */
map->includes_filehdr = (segment->p_offset == 0
&& segment->p_filesz >= iehdr->e_ehsize);
map->includes_phdrs = 0;
if (! phdr_included || segment->p_type != PT_LOAD)
{
map->includes_phdrs =
(segment->p_offset <= (bfd_vma) iehdr->e_phoff
&& (segment->p_offset + segment->p_filesz
>= ((bfd_vma) iehdr->e_phoff
+ iehdr->e_phnum * iehdr->e_phentsize)));
if (segment->p_type == PT_LOAD && map->includes_phdrs)
phdr_included = TRUE;
}
if (map->includes_filehdr && first_section)
/* We need to keep the space used by the headers fixed. */
map->header_size = first_section->vma - segment->p_vaddr;
if (!map->includes_phdrs
&& !map->includes_filehdr
&& map->p_paddr_valid)
/* There is some other padding before the first section. */
map->p_vaddr_offset = ((lowest_section ? lowest_section->lma : 0)
- segment->p_paddr); |
|
|