[rfc] Code cleanup: Permit BIAS to be NULL
Jan Kratochvil
jan.kratochvil at redhat.com
Tue Oct 9 08:56:26 UTC 2012
Hi,
I somehow guess this patch gets rejected so I have not written a ChangeLog.
Sure np to get the patch dropped.
I find using the functions easier this way, it already saves 5 LoC and it is
rather more convenient for elfutils applications.
Thanks,
Jan
diff --git a/libdwfl/derelocate.c b/libdwfl/derelocate.c
index 56f998c..c29aa6e 100644
--- a/libdwfl/derelocate.c
+++ b/libdwfl/derelocate.c
@@ -278,8 +278,7 @@ check_module (Dwfl_Module *mod)
if (mod->dw == NULL)
{
- Dwarf_Addr bias;
- if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL)
+ if (INTUSE(dwfl_module_getdwarf) (mod, NULL) == NULL)
{
Dwfl_Error error = dwfl_errno ();
if (error != DWFL_E_NO_DWARF)
diff --git a/libdwfl/dwfl_module_dwarf_cfi.c b/libdwfl/dwfl_module_dwarf_cfi.c
index 5182d6a..3a4a9c3 100644
--- a/libdwfl/dwfl_module_dwarf_cfi.c
+++ b/libdwfl/dwfl_module_dwarf_cfi.c
@@ -60,7 +60,8 @@ dwfl_module_dwarf_cfi (mod, bias)
if (mod->dwarf_cfi != NULL)
{
- *bias = dwfl_adjusted_dwarf_addr (mod, 0);
+ if (bias)
+ *bias = dwfl_adjusted_dwarf_addr (mod, 0);
return mod->dwarf_cfi;
}
diff --git a/libdwfl/dwfl_module_eh_cfi.c b/libdwfl/dwfl_module_eh_cfi.c
index da10d9f..9cd11db 100644
--- a/libdwfl/dwfl_module_eh_cfi.c
+++ b/libdwfl/dwfl_module_eh_cfi.c
@@ -39,7 +39,8 @@ dwfl_module_eh_cfi (mod, bias)
if (mod->eh_cfi != NULL)
{
- *bias = dwfl_adjusted_address (mod, 0);
+ if (bias)
+ *bias = dwfl_adjusted_address (mod, 0);
return mod->eh_cfi;
}
@@ -50,7 +51,8 @@ dwfl_module_eh_cfi (mod, bias)
return NULL;
}
- *bias = dwfl_adjusted_address (mod, 0);
+ if (bias)
+ *bias = dwfl_adjusted_address (mod, 0);
return __libdwfl_set_cfi (mod, &mod->eh_cfi,
INTUSE(dwarf_getcfi_elf) (mod->main.elf));
}
diff --git a/libdwfl/dwfl_module_getdwarf.c b/libdwfl/dwfl_module_getdwarf.c
index bbf9ff3..fc111df 100644
--- a/libdwfl/dwfl_module_getdwarf.c
+++ b/libdwfl/dwfl_module_getdwarf.c
@@ -1047,7 +1047,8 @@ dwfl_module_getdwarf (Dwfl_Module *mod, Dwarf_Addr *bias)
(void) __libdwfl_relocate (mod, mod->debug.elf, false);
}
- *bias = dwfl_adjusted_dwarf_addr (mod, 0);
+ if (bias)
+ *bias = dwfl_adjusted_dwarf_addr (mod, 0);
return mod->dw;
}
diff --git a/libdwfl/dwfl_module_getelf.c b/libdwfl/dwfl_module_getelf.c
index f20fb04..f88d8d8 100644
--- a/libdwfl/dwfl_module_getelf.c
+++ b/libdwfl/dwfl_module_getelf.c
@@ -57,7 +57,8 @@ dwfl_module_getelf (Dwfl_Module *mod, GElf_Addr *loadbase)
}
}
- *loadbase = dwfl_adjusted_address (mod, 0);
+ if (loadbase)
+ *loadbase = dwfl_adjusted_address (mod, 0);
return mod->main.elf;
}
diff --git a/libdwfl/dwfl_module_getsrc_file.c b/libdwfl/dwfl_module_getsrc_file.c
index 20aa8a5..edc466c 100644
--- a/libdwfl/dwfl_module_getsrc_file.c
+++ b/libdwfl/dwfl_module_getsrc_file.c
@@ -40,8 +40,7 @@ dwfl_module_getsrc_file (Dwfl_Module *mod,
if (mod->dw == NULL)
{
- Dwarf_Addr bias;
- if (INTUSE(dwfl_module_getdwarf) (mod, &bias) == NULL)
+ if (INTUSE(dwfl_module_getdwarf) (mod, NULL) == NULL)
return -1;
}
diff --git a/libdwfl/dwfl_nextcu.c b/libdwfl/dwfl_nextcu.c
index 9ea8388..3295ccb 100644
--- a/libdwfl/dwfl_nextcu.c
+++ b/libdwfl/dwfl_nextcu.c
@@ -54,7 +54,8 @@ dwfl_nextcu (Dwfl *dwfl, Dwarf_Die *lastcu, Dwarf_Addr *bias)
if (cu != NULL)
{
- *bias = dwfl_adjusted_dwarf_addr (mod, 0);
+ if (bias)
+ *bias = dwfl_adjusted_dwarf_addr (mod, 0);
return &cu->die;
}
diff --git a/libdwfl/libdwfl.h b/libdwfl/libdwfl.h
index 506a2a0..60f4e75 100644
--- a/libdwfl/libdwfl.h
+++ b/libdwfl/libdwfl.h
@@ -402,7 +402,8 @@ extern int dwfl_validate_address (Dwfl *dwfl,
/* Fetch the module main ELF file (where the allocated sections
are found) for use with libelf. If successful, fills in *BIAS
with the difference between addresses within the loaded module
- and those in symbol tables or Dwarf information referring to it. */
+ and those in symbol tables or Dwarf information referring to it.
+ BIAS may be passed as NULL. */
extern Elf *dwfl_module_getelf (Dwfl_Module *, GElf_Addr *bias);
/* Return the number of symbols in the module's symbol table,
@@ -443,9 +444,9 @@ extern Elf_Scn *dwfl_module_address_section (Dwfl_Module *mod,
/* Fetch the module's debug information for use with libdw.
If successful, fills in *BIAS with the difference between
- addresses within the loaded module and those to use with libdw. */
-extern Dwarf *dwfl_module_getdwarf (Dwfl_Module *, Dwarf_Addr *bias)
- __nonnull_attribute__ (2);
+ addresses within the loaded module and those to use with libdw.
+ BIAS may be pased as NULL. */
+extern Dwarf *dwfl_module_getdwarf (Dwfl_Module *, Dwarf_Addr *bias);
/* Get the libdw handle for each module. */
extern ptrdiff_t dwfl_getdwarf (Dwfl *,
@@ -455,24 +456,21 @@ extern ptrdiff_t dwfl_getdwarf (Dwfl *,
void *arg, ptrdiff_t offset);
/* Look up the module containing ADDR and return its debugging information,
- loading it if necessary. */
-extern Dwarf *dwfl_addrdwarf (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Addr *bias)
- __nonnull_attribute__ (3);
+ loading it if necessary. BIAS may be passed as NULL. */
+extern Dwarf *dwfl_addrdwarf (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Addr *bias);
-/* Find the CU containing ADDR and return its DIE. */
-extern Dwarf_Die *dwfl_addrdie (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Addr *bias)
- __nonnull_attribute__ (3);
+/* Find the CU containing ADDR and return its DIE.
+ BIAS may be passed as NULL. */
+extern Dwarf_Die *dwfl_addrdie (Dwfl *dwfl, Dwarf_Addr addr, Dwarf_Addr *bias);
extern Dwarf_Die *dwfl_module_addrdie (Dwfl_Module *mod,
- Dwarf_Addr addr, Dwarf_Addr *bias)
- __nonnull_attribute__ (3);
+ Dwarf_Addr addr, Dwarf_Addr *bias);
-/* Iterate through the CUs, start with null for LASTCU. */
-extern Dwarf_Die *dwfl_nextcu (Dwfl *dwfl, Dwarf_Die *lastcu, Dwarf_Addr *bias)
- __nonnull_attribute__ (3);
+/* Iterate through the CUs, start with null for LASTCU. BIAS may be passed as
+ NULL. */
+extern Dwarf_Die *dwfl_nextcu (Dwfl *dwfl, Dwarf_Die *lastcu, Dwarf_Addr *bias);
extern Dwarf_Die *dwfl_module_nextcu (Dwfl_Module *mod,
- Dwarf_Die *lastcu, Dwarf_Addr *bias)
- __nonnull_attribute__ (3);
+ Dwarf_Die *lastcu, Dwarf_Addr *bias);
/* Return the module containing the CU DIE. */
extern Dwfl_Module *dwfl_cumodule (Dwarf_Die *cudie);
@@ -546,8 +544,9 @@ extern int dwfl_module_register_names (Dwfl_Module *mod,
/* Find the CFI for this module. Returns NULL if there is no CFI.
On success, fills in *BIAS with the difference between addresses
within the loaded module and those in the CFI referring to it.
- The pointer returned can be used until the module is cleaned up.
- Calling these more than once returns the same pointers.
+ BIAS may be also passed as NULL. The pointer returned can be used
+ until the module is cleaned up. Calling these more than once returns
+ the same pointers.
dwfl_module_dwarf_cfi gets the '.debug_frame' information found with the
rest of the DWARF information. dwfl_module_eh_cfi gets the '.eh_frame'
diff --git a/src/readelf.c b/src/readelf.c
index 5d167eb..be53983 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -613,8 +613,7 @@ process_file (int fd, const char *fname, bool only_one)
static void
process_elf_file (Dwfl_Module *dwflmod, int fd)
{
- GElf_Addr dwflbias;
- Elf *elf = dwfl_module_getelf (dwflmod, &dwflbias);
+ Elf *elf = dwfl_module_getelf (dwflmod, NULL);
GElf_Ehdr ehdr_mem;
GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
@@ -7246,8 +7245,7 @@ static void
print_debug (Dwfl_Module *dwflmod, Ebl *ebl, GElf_Ehdr *ehdr)
{
/* Before we start the real work get a debug context descriptor. */
- Dwarf_Addr dwbias;
- Dwarf *dbg = dwfl_module_getdwarf (dwflmod, &dwbias);
+ Dwarf *dbg = dwfl_module_getdwarf (dwflmod, NULL);
Dwarf dummy_dbg =
{
.elf = ebl->elf,
diff --git a/src/unstrip.c b/src/unstrip.c
index f6660a3..acfa2d0 100644
--- a/src/unstrip.c
+++ b/src/unstrip.c
@@ -1979,8 +1979,7 @@ static void
handle_dwfl_module (const char *output_file, bool create_dirs,
Dwfl_Module *mod, bool all, bool ignore, bool relocate)
{
- GElf_Addr bias;
- Elf *stripped = dwfl_module_getelf (mod, &bias);
+ Elf *stripped = dwfl_module_getelf (mod, NULL);
if (stripped == NULL)
{
if (ignore)
@@ -1999,7 +1998,7 @@ handle_dwfl_module (const char *output_file, bool create_dirs,
modname, file, dwfl_errmsg (-1));
}
- Elf *debug = dwarf_getelf (dwfl_module_getdwarf (mod, &bias));
+ Elf *debug = dwarf_getelf (dwfl_module_getdwarf (mod, NULL));
if (debug == NULL && !all)
{
if (ignore)
@@ -2074,8 +2073,7 @@ handle_output_dir_module (const char *output_dir, Dwfl_Module *mod,
if (! modnames)
{
/* Make sure we've searched for the ELF file. */
- GElf_Addr bias;
- (void) dwfl_module_getelf (mod, &bias);
+ (void) dwfl_module_getelf (mod, NULL);
}
const char *file;
@@ -2097,9 +2095,8 @@ static void
list_module (Dwfl_Module *mod)
{
/* Make sure we have searched for the files. */
- GElf_Addr bias;
- bool have_elf = dwfl_module_getelf (mod, &bias) != NULL;
- bool have_dwarf = dwfl_module_getdwarf (mod, &bias) != NULL;
+ bool have_elf = dwfl_module_getelf (mod, NULL) != NULL;
+ bool have_dwarf = dwfl_module_getdwarf (mod, NULL) != NULL;
const char *file;
const char *debug;
@@ -2160,8 +2157,7 @@ match_module (Dwfl_Module *mod,
if (info->match_files)
{
/* Make sure we've searched for the ELF file. */
- GElf_Addr bias;
- (void) dwfl_module_getelf (mod, &bias);
+ (void) dwfl_module_getelf (mod, NULL);
const char *file;
const char *check = dwfl_module_info (mod, NULL, NULL, NULL,
diff --git a/tests/allregs.c b/tests/allregs.c
index b103ce1..8f77216 100644
--- a/tests/allregs.c
+++ b/tests/allregs.c
@@ -54,8 +54,7 @@ first_module (Dwfl_Module *mod,
Dwarf_Addr low_addr __attribute__ ((unused)),
void *arg)
{
- Dwarf_Addr bias;
- if (dwfl_module_getelf (mod, &bias) == NULL) /* Not really a module. */
+ if (dwfl_module_getelf (mod, NULL) == NULL) /* Not really a module. */
return DWARF_CB_OK;
*(Dwfl_Module **) arg = mod;
diff --git a/tests/dwfl-bug-fd-leak.c b/tests/dwfl-bug-fd-leak.c
index 170a61a..afbf95e 100644
--- a/tests/dwfl-bug-fd-leak.c
+++ b/tests/dwfl-bug-fd-leak.c
@@ -55,8 +55,7 @@ elfutils_open (pid_t pid, Dwarf_Addr address)
if (dwfl_report_end (dwfl, NULL, NULL) != 0)
error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
- Dwarf_Addr bias;
- Dwarf *dbg = dwfl_addrdwarf (dwfl, address, &bias);
+ Dwarf *dbg = dwfl_addrdwarf (dwfl, address, NULL);
if (dbg != NULL)
{
Elf *elf = dwarf_getelf (dbg);
@@ -65,7 +64,7 @@ elfutils_open (pid_t pid, Dwarf_Addr address)
}
else
{
- Elf *elf = dwfl_module_getelf (dwfl_addrmodule (dwfl, address), &bias);
+ Elf *elf = dwfl_module_getelf (dwfl_addrmodule (dwfl, address), NULL);
if (elf == NULL)
error (2, 0, "dwfl_module_getelf: %s", dwfl_errmsg (-1));
}
More information about the elfutils-devel
mailing list