Re: File index given line (libdw)

2017-07-14 Thread Mark Wielaard
On Thu, 2017-07-13 at 17:33 +, Sasha Da Rocha Pinheiro wrote:
> given a Die, I want to associate each line (from dwarf_getsrclines) to
> a file (from dwarf_getsrcfiles). The only way to do this is get the
> filename returned by dwarf_linesrc, and search in the filenames given
> by dwarf_getsrcfiles and dwarf_filesrc. But this is expensive since
> this has to be done for each line. Instead, if I could get the file
> index in the array "struct Dwarf_Fileinfo_s info", I wouldn't need to
> search.

But why do you want to do that?
If we would add int dwarf_line_index (Dwarf_Line *line, size_t *idx) how
exactly would you use it?
What would be a good testcase for this new functionality?
A small example program would help to see what the exact semantics
should be.

Thanks,

Mark


[PATCH] strip: Add --keep-section=SECTION and --remove-section=SECTION.

2017-07-14 Thread Mark Wielaard
Adds two new output options:

  --keep-section=SECTION Keep the named section.  SECTION is an extended
 wildcard pattern.  May be given more than once.

  --remove-section=SECTION   Remove the named section.  SECTION is an
 extended wildcard pattern.  May be given more than
 once.  Only non-allocated sections can be removed.

The --remove-section was already partially implemented, but only for the
.comment section. The short option -R is to be compatible with binutils.

The new testcase makes sure that various combinations of kept/removed
sections pull the correct dependencies into the output and/or debug files.

Signed-off-by: Mark Wielaard 
---
 ChangeLog  |   4 +
 NEWS   |   4 +
 src/ChangeLog  |  13 +
 src/strip.c| 111 ++-
 tests/ChangeLog|   6 +
 tests/Makefile.am  |   4 +-
 tests/run-strip-remove-keep.sh | 688 +
 7 files changed, 819 insertions(+), 11 deletions(-)
 create mode 100755 tests/run-strip-remove-keep.sh

diff --git a/ChangeLog b/ChangeLog
index 0d6cd2b..c9db732 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2017-07-14  Mark Wielaard  
+
+   * NEWS: Add 0.170 section and new strip options.
+
 2017-05-05  Mark Wielaard  
 
* configure.ac: Set version to 0.169. Update copyright year.
diff --git a/NEWS b/NEWS
index eb7dd97..b69aef4 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+Version 0.170
+
+strip: Add -R, --remove-section=SECTION and --keep-section=SECTION.
+
 Version 0.169
 
 backends: Add support for EM_PPC64 GNU_ATTRIBUTES.
diff --git a/src/ChangeLog b/src/ChangeLog
index e19122e..0b60fc7 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,16 @@
+2017-07-14  Mark Wielaard  
+
+   * strip (OPT_KEEP_SECTION): New define.
+   (options): Add documentation for remove-section. Add keep-section.
+   (struct section_pattern): New data type.
+   (keep_secs, remove_secs): New globals.
+   (add_pattern, free_sec_patterns, free_patterns, section_name_matches):
+   New functions.
+   (main): Call free_patterns.
+   (parse_opt): Handle 'R' and OPT_KEEP_SECTION. Check remove_comment
+   on ARGP_KEY_SUCCESS.
+   (handle_elf): Handle and sanity check keep_secs and remove_secs.
+
 2017-06-07  Mark Wielaard  
 
* strip.c (handle_elf): Introduce new handle_elf boolean. Use it to
diff --git a/src/strip.c b/src/strip.c
index 2bf95f9..3aad92e 100644
--- a/src/strip.c
+++ b/src/strip.c
@@ -26,6 +26,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -60,6 +61,7 @@ ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
 #define OPT_PERMISSIVE 0x101
 #define OPT_STRIP_SECTIONS 0x102
 #define OPT_RELOC_DEBUG0x103
+#define OPT_KEEP_SECTION   0x104
 
 
 /* Definitions of arguments for argp functions.  */
@@ -83,7 +85,8 @@ static const struct argp_option options[] =
 N_("Resolve all trivial relocations between debug sections if the removed 
sections are placed in a debug file (only relevant for ET_REL files, operation 
is not reversable, needs -f)"), 0 },
   { "remove-comment", OPT_REMOVE_COMMENT, NULL, 0,
 N_("Remove .comment section"), 0 },
-  { "remove-section", 'R', "SECTION", OPTION_HIDDEN, NULL, 0 },
+  { "remove-section", 'R', "SECTION", 0, N_("Remove the named section.  
SECTION is an extended wildcard pattern.  May be given more than once.  Only 
non-allocated sections can be removed."), 0 },
+  { "keep-section", OPT_KEEP_SECTION, "SECTION", 0, N_("Keep the named 
section.  SECTION is an extended wildcard pattern.  May be given more than 
once."), 0 },
   { "permissive", OPT_PERMISSIVE, NULL, 0,
 N_("Relax a few rules to handle slightly broken ELF files"), 0 },
   { NULL, 0, NULL, 0, NULL, 0 }
@@ -157,6 +160,58 @@ static bool permissive;
 /* If true perform relocations between debug sections.  */
 static bool reloc_debug;
 
+/* Sections the user explicitly wants to keep or remove.  */
+struct section_pattern
+{
+  char *pattern;
+  struct section_pattern *next;
+};
+
+static struct section_pattern *keep_secs = NULL;
+static struct section_pattern *remove_secs = NULL;
+
+static void
+add_pattern (struct section_pattern **patterns, const char *pattern)
+{
+  struct section_pattern *p = xmalloc (sizeof *p);
+  p->pattern = xstrdup (pattern);
+  p->next = *patterns;
+  *patterns = p;
+}
+
+static void
+free_sec_patterns (struct section_pattern *patterns)
+{
+  struct section_pattern *pattern = patterns;
+  while (pattern != NULL)
+{
+  struct section_pattern *p = pattern;
+  pattern = p->next;
+  free (p->pattern);
+  free (p);
+}
+}
+
+static void
+free_patterns (void)
+{
+  free_sec_patterns (keep_secs);
+  free_sec_patterns (remove_secs);
+}
+
+static bool
+section_name_matches (struct section_pattern *patterns, const char *name)

Re: [PATCH] strip: Add --keep-section=SECTION and --remove-section=SECTION.

2017-07-14 Thread Josh Stone
On 07/14/2017 08:28 AM, Mark Wielaard wrote:
> Adds two new output options:
> 
>   --keep-section=SECTION Keep the named section.  SECTION is an extended
>  wildcard pattern.  May be given more than once.

I tried this with rust libraries (eu-strip --keep-section=.rustc), and
it seems to work as desired.  Thanks!


Dwarf_FDE (libdw)

2017-07-14 Thread Sasha Da Rocha Pinheiro
Hi,
I did not understand how to get the augmentation_data of a FDE. Could anyone 
explain me?
Also, is the start and end of Dwarf_FDE to be used as [initial_location, 
initial_location+address_range)??

Regards,
Sasha

typedef struct
{
  /* Section offset of CIE this FDE refers to.  This will never be
 DW_CIE_ID_64 in an FDE.  If this value is DW_CIE_ID_64, this is
 actually a Dwarf_CIE structure.  */
  Dwarf_Off CIE_pointer;

  /* We can't really decode anything further without looking up the CIE
 and checking its augmentation string.  Here follows the encoded
 initial_location and address_range, then any augmentation data,
 then the instruction stream.  This FDE describes PC locations in
 the byte range [initial_location, initial_location+address_range).
 When the CIE augmentation string uses 'z', the augmentation data is
 a DW_FORM_block (self-sized).  Otherwise, when we understand the
 augmentation string completely, fde_augmentation_data_size gives
 the number of bytes of augmentation data before the instructions.  */
  const uint8_t *start;
  const uint8_t *end;
} Dwarf_FDE;


Re: File index given line (libdw)

2017-07-14 Thread Sasha Da Rocha Pinheiro
Hi Mark,

>But why do you want to do that?

Performance and save memory space.


>If we would add int dwarf_line_index (Dwarf_Line *line, size_t *idx) how
>exactly would you use it?

I would get idx and save it in my data structure so I don't have to save the 
file name repeatedly for each line.


>What would be a good testcase for this new functionality?
This new function would behave similar to:

int dwarf_lineno (Dwarf_Line *line, int *linep)
{
  if (line == NULL)
return -1;

  *linep =  line->line;

  return 0;
}

But would get line->file instead of line->line;
This way, since I already got the files previously, I now can refer to some 
line file source by its id.


>A small example program would help to see what the exact semantics
>should be.
How it's currently being done :

1. Dwarf_Files dfs <- dwarf_getsrcfiles
2. for each file in dfs get name (with dwarf_filesrc) -> save in vector 
filenames
3. Dwarf_Lines dls <- dwarf_getsrclines
4. for each line in dls get file name and "search this name in vector filenames"

We want to eliminate the "search this name in vector filenames", to make it 
from L F log(F) to L, by getting the index and consulting the file name of a 
line in the vector filenames directly.

Regards,
Sasha