From: Simon Marchi Date: Tue, 19 May 2020 16:18:04 +0000 (-0400) Subject: gdb: use std::vector to store segments in symfile_segment_data X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=68b888fff3164b5e8e347d9c1ca351c366f0aac4;p=binutils-gdb.git gdb: use std::vector to store segments in symfile_segment_data Instead of maintaining two vectors, I added a small `segment` class which holds both the base address and size of one segment and replaced the two `segment_bases` and `segment_sizes` arrays with a single vector. The rest of the changes are straightforward, no behavior changes are expected. gdb/ChangeLog: * symfile.h (struct symfile_segment_data) : New. : New. : Remove. * symfile.c (default_symfile_segments): Update. * elfread.c (elf_symfile_segments): Update. * remote.c (remote_target::get_offsets): Update. * solib-target.c (solib_target_relocate_section_addresses): Update. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index ac9e1ccccaa..c9f4a5ebb02 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,14 @@ +2020-05-19 Simon Marchi + + * symfile.h (struct symfile_segment_data) : New. + : New. + : Remove. + * symfile.c (default_symfile_segments): Update. + * elfread.c (elf_symfile_segments): Update. + * remote.c (remote_target::get_offsets): Update. + * solib-target.c (solib_target_relocate_section_addresses): + Update. + 2020-05-19 Simon Marchi * symfile.h (struct symfile_segment_data): Initialize fields. diff --git a/gdb/elfread.c b/gdb/elfread.c index 4804d62074c..4318ebf9eb9 100644 --- a/gdb/elfread.c +++ b/gdb/elfread.c @@ -112,15 +112,10 @@ elf_symfile_segments (bfd *abfd) return NULL; symfile_segment_data_up data (new symfile_segment_data); - data->num_segments = num_segments; - data->segment_bases = XCNEWVEC (CORE_ADDR, num_segments); - data->segment_sizes = XCNEWVEC (CORE_ADDR, num_segments); + data->segments.reserve (num_segments); for (i = 0; i < num_segments; i++) - { - data->segment_bases[i] = segments[i]->p_vaddr; - data->segment_sizes[i] = segments[i]->p_memsz; - } + data->segments.emplace_back (segments[i]->p_vaddr, segments[i]->p_memsz); num_sections = bfd_count_sections (abfd); data->segment_info = XCNEWVEC (int, num_sections); diff --git a/gdb/remote.c b/gdb/remote.c index a28f34d157a..312a03c8fb4 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -4198,10 +4198,10 @@ remote_target::get_offsets () by assuming that the .text and .data offsets apply to the whole text and data segments. Convert the offsets given in the packet to base addresses for symfile_map_offsets_to_segments. */ - else if (data && data->num_segments == 2) + else if (data != nullptr && data->segments.size () == 2) { - segments[0] = data->segment_bases[0] + text_addr; - segments[1] = data->segment_bases[1] + data_addr; + segments[0] = data->segments[0].base + text_addr; + segments[1] = data->segments[1].base + data_addr; num_segments = 2; } /* If the object file has only one segment, assume that it is text @@ -4209,9 +4209,9 @@ remote_target::get_offsets () but programs with no code are useless. Of course the code might have ended up in the data segment... to detect that we would need the permissions here. */ - else if (data && data->num_segments == 1) + else if (data && data->segments.size () == 1) { - segments[0] = data->segment_bases[0] + text_addr; + segments[0] = data->segments[0].base + text_addr; num_segments = 1; } /* There's no way to relocate by segment. */ diff --git a/gdb/solib-target.c b/gdb/solib-target.c index 35e50a3e00b..ba056478c06 100644 --- a/gdb/solib-target.c +++ b/gdb/solib-target.c @@ -386,9 +386,9 @@ Could not relocate shared library \"%s\": bad offsets"), so->so_name); "info sharedlibrary". Report any consecutive segments which were relocated as a single unit. */ gdb_assert (li->segment_bases.size () > 0); - orig_delta = li->segment_bases[0] - data->segment_bases[0]; + orig_delta = li->segment_bases[0] - data->segments[0].base; - for (i = 1; i < data->num_segments; i++) + for (i = 1; i < data->segments.size (); i++) { /* If we have run out of offsets, assume all remaining segments have the same offset. */ @@ -397,14 +397,14 @@ Could not relocate shared library \"%s\": bad offsets"), so->so_name); /* If this segment does not have the same offset, do not include it in the library's range. */ - if (li->segment_bases[i] - data->segment_bases[i] + if (li->segment_bases[i] - data->segments[i].base != orig_delta) break; } so->addr_low = li->segment_bases[0]; - so->addr_high = (data->segment_bases[i - 1] - + data->segment_sizes[i - 1] + so->addr_high = (data->segments[i - 1].base + + data->segments[i - 1].size + orig_delta); gdb_assert (so->addr_low <= so->addr_high); } diff --git a/gdb/symfile.c b/gdb/symfile.c index 9d5e2824b2a..22793e73639 100644 --- a/gdb/symfile.c +++ b/gdb/symfile.c @@ -745,9 +745,6 @@ default_symfile_segments (bfd *abfd) high = low + bfd_section_size (sect); symfile_segment_data_up data (new symfile_segment_data); - data->num_segments = 1; - data->segment_bases = XCNEW (CORE_ADDR); - data->segment_sizes = XCNEW (CORE_ADDR); num_sections = bfd_count_sections (abfd); data->segment_info = XCNEWVEC (int, num_sections); @@ -768,8 +765,7 @@ default_symfile_segments (bfd *abfd) data->segment_info[i] = 1; } - data->segment_bases[0] = low; - data->segment_sizes[0] = high - low; + data->segments.emplace_back (low, high - low); return data; } @@ -3663,13 +3659,13 @@ symfile_map_offsets_to_segments (bfd *abfd, /* If we do not have segment mappings for the object file, we can not relocate it by segments. */ gdb_assert (data != NULL); - gdb_assert (data->num_segments > 0); + gdb_assert (data->segments.size () > 0); for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next) { int which = data->segment_info[i]; - gdb_assert (0 <= which && which <= data->num_segments); + gdb_assert (0 <= which && which <= data->segments.size ()); /* Don't bother computing offsets for sections that aren't loaded as part of any segment. */ @@ -3681,7 +3677,7 @@ symfile_map_offsets_to_segments (bfd *abfd, if (which > num_segment_bases) which = num_segment_bases; - offsets[i] = segment_bases[which - 1] - data->segment_bases[which - 1]; + offsets[i] = segment_bases[which - 1] - data->segments[which - 1].base; } return 1; @@ -3699,7 +3695,7 @@ symfile_find_segment_sections (struct objfile *objfile) if (data == NULL) return; - if (data->num_segments != 1 && data->num_segments != 2) + if (data->segments.size () != 1 && data->segments.size () != 2) return; for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next) diff --git a/gdb/symfile.h b/gdb/symfile.h index 2dfa6556d47..1f2395169af 100644 --- a/gdb/symfile.h +++ b/gdb/symfile.h @@ -80,29 +80,31 @@ typedef std::vector section_addr_info; each BFD section belongs to. */ struct symfile_segment_data { + struct segment + { + segment (CORE_ADDR base, CORE_ADDR size) + : base (base), size (size) + {} + + /* The original base address the segment. */ + CORE_ADDR base; + + /* The memory size of the segment. */ + CORE_ADDR size; + }; + ~symfile_segment_data () { - xfree (this->segment_bases); - xfree (this->segment_sizes); xfree (this->segment_info); } - /* How many segments are present in this file. If there are + /* The segments present in this file. If there are two, the text segment is the first one and the data segment is the second one. */ - int num_segments = 0; - - /* If NUM_SEGMENTS is greater than zero, the original base address - of each segment. */ - CORE_ADDR *segment_bases = nullptr; - - /* If NUM_SEGMENTS is greater than zero, the memory size of each - segment. */ - CORE_ADDR *segment_sizes = nullptr; + std::vector segments; - /* If NUM_SEGMENTS is greater than zero, this is an array of entries - recording which segment contains each BFD section. - SEGMENT_INFO[I] is S+1 if the I'th BFD section belongs to segment + /* This is an array of entries recording which segment contains each BFD + section. SEGMENT_INFO[I] is S+1 if the I'th BFD section belongs to segment S, or zero if it is not in any segment. */ int *segment_info = nullptr; };