From: Simon Marchi <simon.marchi@efficios.com>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@efficios.com>
Subject: [PATCH 2/3] gdb: use std::vector to store segments in symfile_segment_data
Date: Mon, 18 May 2020 21:35:32 -0400 [thread overview]
Message-ID: <20200519013533.1428830-3-simon.marchi@efficios.com> (raw)
In-Reply-To: <20200519013533.1428830-1-simon.marchi@efficios.com>
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) <struct segment>: New.
<segments>: New.
<segment_bases, segment_sizes>: 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.
---
gdb/elfread.c | 8 +-------
gdb/remote.c | 10 +++++-----
gdb/solib-target.c | 10 +++++-----
gdb/symfile.c | 14 +++++---------
gdb/symfile.h | 32 +++++++++++++++++---------------
5 files changed, 33 insertions(+), 41 deletions(-)
diff --git a/gdb/elfread.c b/gdb/elfread.c
index 4804d62074c4..822f443fa8d3 100644
--- a/gdb/elfread.c
+++ b/gdb/elfread.c
@@ -112,15 +112,9 @@ 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);
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 a28f34d157a9..312a03c8fb42 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 35e50a3e00b4..ba056478c061 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 9d5e2824b2a7..22793e736398 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 2dfa6556d479..1f2395169af9 100644
--- a/gdb/symfile.h
+++ b/gdb/symfile.h
@@ -80,29 +80,31 @@ typedef std::vector<other_sections> 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<segment> 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;
};
--
2.26.2
next prev parent reply other threads:[~2020-05-19 1:35 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-05-19 1:35 [PATCH 0/3] C++ification of symfile_segment_data Simon Marchi
2020-05-19 1:35 ` [PATCH 1/3] gdb: allocate symfile_segment_data with new Simon Marchi
2020-05-19 1:35 ` Simon Marchi [this message]
2020-05-19 14:55 ` [PATCH 2/3] gdb: use std::vector to store segments in symfile_segment_data Tom Tromey
2020-05-19 14:52 ` Simon Marchi
2020-05-19 15:44 ` Tom Tromey
2020-05-19 1:35 ` [PATCH 3/3] gdb: make symfile_segment_data::segment_info an std::vector Simon Marchi
2020-05-19 14:57 ` [PATCH 0/3] C++ification of symfile_segment_data Tom Tromey
2020-05-19 16:25 ` Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200519013533.1428830-3-simon.marchi@efficios.com \
--to=simon.marchi@efficios.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox