From 46464c374b9a890a42eaab2a64973300c5531cdb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vivek=20Das=C2=A0Mohapatra?= <vivek@collabora.co.uk>
Date: Tue, 16 May 2017 20:10:39 +0100
Subject: [PATCH] Use the new debug flags to control debugging

---
 capsule/capsule-dlmopen.c  | 115 ++++++++++++++++++------------------
 capsule/capsule-relocate.c |  20 +++----
 capsule/capsule.h          |   4 +-
 utils/debug.h              |  12 ++--
 utils/process-pt-dynamic.c | 116 +++++++++++++++----------------------
 5 files changed, 125 insertions(+), 142 deletions(-)

diff --git a/capsule/capsule-dlmopen.c b/capsule/capsule-dlmopen.c
index 348234028..916fab154 100644
--- a/capsule/capsule-dlmopen.c
+++ b/capsule/capsule-dlmopen.c
@@ -33,23 +33,12 @@
 #include <gelf.h>
 #include <dlfcn.h>
 
-#define DEBUG
-
 #include "capsule.h"
 #include "utils/utils.h"
 #include "utils/dump.h"
 #include "utils/mmap-info.h"
 #include "utils/process-pt-dynamic.h"
 
-#ifdef DEBUG
-#define ldlib_debug(l, fmt, args...) \
-    if( (l)->debug )                 \
-        fprintf( stderr, "%s:" fmt "\n", __PRETTY_FUNCTION__, ##args )
-#else
-#define ldlib_debug(fmt, args...) \
-
-#endif
-
 // ==========================================================================
 // this is stolen from the ld.so config (dl-cache.h) since we need
 // to use a slightly re-brained version of the linker to do our
@@ -186,7 +175,7 @@ typedef struct
     char *not_found[DSO_LIMIT];
     char *error;
     int last_not_found;
-    int debug;
+    unsigned long debug;
 } ldlibs_t;
 
 typedef int (*ldcache_entry_cb) (ldlibs_t *ldlibs,
@@ -277,15 +266,14 @@ void resolve_symlink_prefixed (ldlibs_t *ldlibs, int i)
         (ldlibs->prefix.path[0] == '/' && ldlibs->prefix.path[1] == '\0') )
         return;
 
-    if( ldlibs->debug )
-        debug( "resolving (un)prefixed link in %s", ldlibs->needed[i].path );
+    LDLIB_DEBUG( ldlibs, DEBUG_PATH,
+                 "resolving (un)prefixed link in %s", ldlibs->needed[i].path );
 
     safe_strncpy( resolved, ldlibs->needed[i].path, PATH_MAX );
 
     while( resolve_link(ldlibs->prefix.path, resolved, link_dir) )
     {
-        if( ldlibs->debug )
-            debug( "  resolved to: %s", resolved );
+        LDLIB_DEBUG( ldlibs, DEBUG_PATH, "  resolved to: %s", resolved );
         count++;
     }
 
@@ -302,15 +290,14 @@ void resolve_symlink_prefixed (ldlibs_t *ldlibs, int i)
 static int
 ldlib_open (ldlibs_t *ldlibs, const char *name, int i)
 {
-    const int dflag = ldlibs->debug;
 
-    ldlib_debug( ldlibs, "ldlib_open: target -: %s", ldlibs->needed[i].path );
+    LDLIB_DEBUG( ldlibs, DEBUG_SEARCH,
+                 "ldlib_open: target -: %s", ldlibs->needed[i].path );
 
-    ldlibs->debug = 0;
     resolve_symlink_prefixed( ldlibs, i );
-    ldlibs->debug = dflag;
 
-    ldlib_debug( ldlibs, "ldlib_open: target +: %s", ldlibs->needed[i].path );
+    LDLIB_DEBUG( ldlibs, DEBUG_SEARCH,
+                 "ldlib_open: target +: %s", ldlibs->needed[i].path );
 
     ldlibs->needed[i].fd = open( ldlibs->needed[i].path, O_RDONLY );
 
@@ -327,7 +314,8 @@ ldlib_open (ldlibs_t *ldlibs, const char *name, int i)
         else
             acceptable = check_elf_constraints( ldlibs, i );
 
-        ldlib_debug( ldlibs, "[%03d] %s on fd #%d; elf: %p; acceptable: %d",
+        LDLIB_DEBUG( ldlibs, DEBUG_SEARCH,
+                     "[%03d] %s on fd #%d; elf: %p; acceptable: %d",
                      i,
                      ldlibs->needed[i].path,
                      ldlibs->needed[i].fd  ,
@@ -374,7 +362,8 @@ iterate_ldcache (ldlibs_t *ldlibs, ldcache_entry_cb cb, void *data)
         break;
 
       default:
-        debug("Invalid ld cache type, cannot parse");
+        fprintf( stderr, "Invalid ld cache type %d, cannot parse",
+                 ldlibs->ctype );
         exit(22);
     }
 
@@ -457,7 +446,9 @@ search_ldpath (const char *name, const char *ldpath, ldlibs_t *ldlibs, int i)
     prefix[plen] = '\0';
 
     sanitise_ldlibs(ldlibs);
-    ldlib_debug( ldlibs, "searching for %s in %s (prefix: %s)",
+
+    LDLIB_DEBUG( ldlibs, DEBUG_SEARCH,
+                 "searching for %s in %s (prefix: %s)",
                  name, ldpath, plen ? prefix : "-none-" );
 
     while( sp && *sp )
@@ -474,7 +465,7 @@ search_ldpath (const char *name, const char *ldpath, ldlibs_t *ldlibs, int i)
         safe_strncpy( prefix + plen, sp, len + 1);
         prefix[plen + len + 1] = '\0';
 
-        ldlib_debug( ldlibs, "  searchpath element: %s", prefix );
+        LDLIB_DEBUG( ldlibs, DEBUG_SEARCH, "  searchpath element: %s", prefix );
         // append the target name, without overflowing, then resolve
         if( (plen + len + strlen( name ) + 1 < PATH_MAX) )
         {
@@ -482,7 +473,7 @@ search_ldpath (const char *name, const char *ldpath, ldlibs_t *ldlibs, int i)
             safe_strncpy( prefix + plen + len + 1, name,
                           PATH_MAX - plen - len - 1 );
 
-            ldlib_debug( ldlibs, "examining %s", prefix );
+            LDLIB_DEBUG( ldlibs, DEBUG_SEARCH, "examining %s", prefix );
             if( realpath( prefix, ldlibs->needed[i].path ) &&
                 ldlib_open( ldlibs, name, i ) )
                 return 1;
@@ -544,7 +535,7 @@ dso_find (const char *name, ldlibs_t *ldlibs, int i)
             target = name;
         }
 
-        ldlib_debug( ldlibs, "resolving path %s", target );
+        LDLIB_DEBUG( ldlibs, DEBUG_PATH, "resolving path %s", target );
         if( realpath( target, ldlibs->needed[i].path ) )
             return ldlib_open( ldlibs, name, i );
     }
@@ -605,7 +596,8 @@ _dso_iterate_sections (ldlibs_t *ldlibs, int idx)
     ldlibs->needed[idx].dso =
       elf_begin( ldlibs->needed[idx].fd, ELF_C_READ_MMAP, NULL );
 
-    ldlib_debug( ldlibs, "%03d: fd:%d dso:%p ← %s",
+    LDLIB_DEBUG( ldlibs, DEBUG_CAPSULE,
+                 "%03d: fd:%d dso:%p ← %s",
                  idx,
                  ldlibs->needed[idx].fd,
                  ldlibs->needed[idx].dso,
@@ -650,7 +642,8 @@ _dso_iterate_sections (ldlibs_t *ldlibs, int idx)
                 {
                     if( strcmp( *x, next_dso ) == 0 )
                     {
-                        ldlib_debug( ldlibs, "skipping %s / %s", next_dso, *x );
+                        LDLIB_DEBUG( ldlibs, DEBUG_CAPSULE|DEBUG_SEARCH,
+                                     "skipping %s / %s", next_dso, *x );
                         skip = 1;
                         break;
                     }
@@ -978,7 +971,9 @@ load_ldlibs (ldlibs_t *ldlibs, Lmid_t *namespace, int flag, int *errcode, char *
                 const char *path = ldlibs->needed[j].path;
                 go++;
 
-                ldlib_debug( ldlibs, "DLMOPEN %p %s %s", (void *)lm, _rtldstr(flag), path );
+                LDLIB_DEBUG( ldlibs, DEBUG_CAPSULE,
+                             "DLMOPEN %p %s %s",
+                             (void *)lm, _rtldstr(flag), path );
 
                 // The actual dlmopen. If this was the first one, it may
                 // have created a new link map id, wich we record later on:
@@ -1005,7 +1000,8 @@ load_ldlibs (ldlibs_t *ldlibs, Lmid_t *namespace, int flag, int *errcode, char *
                 {
                     dlinfo( ret, RTLD_DI_LMID, namespace );
                     lm = *namespace;
-                    ldlib_debug( ldlibs, "new Lmid_t handle %p\n", (void *)lm );
+                    LDLIB_DEBUG( ldlibs, DEBUG_CAPSULE,
+                                 "new Lmid_t handle %p\n", (void *)lm );
                 }
 
                 // go through the map of DSOs and reduce the dependency
@@ -1026,7 +1022,7 @@ static void
 init_ldlibs (ldlibs_t *ldlibs,
              const char **exclude,
              const char *prefix,
-             int dbg,
+             unsigned long dbg,
              int *errcode,
              char **error)
 {
@@ -1180,8 +1176,7 @@ static void
 wrap (const char *name,
       ElfW(Addr) base,
       ElfW(Dyn) *dyn,
-      capsule_item_t *wrappers,
-      int dbg)
+      capsule_item_t *wrappers)
 {
     int mmap_errno = 0;
     char *mmap_error = NULL;
@@ -1194,16 +1189,18 @@ wrap (const char *name,
     relocation_data_t rdata = { 0 };
 
     rdata.target    = name;
-    rdata.debug     = dbg;
+    rdata.debug     = debug_flags;
     rdata.error     = NULL;
     rdata.relocs    = wrappers;
     rdata.mmap_info = load_mmap_info( &mmap_errno, &mmap_error );
 
-    if( dbg && (mmap_errno || mmap_error) )
+    if( mmap_errno || mmap_error )
     {
-        debug("mmap/mprotect flags information load error (errno: %d): %s",
-              mmap_errno, mmap_error );
-        debug("relocation will be unable to handle relro linked libraries");
+        DEBUG( DEBUG_MPROTECT,
+               "mmap/mprotect flags information load error (errno: %d): %s",
+               mmap_errno, mmap_error );
+        DEBUG( DEBUG_MPROTECT,
+               "relocation will be unable to handle relro linked libraries" );
     }
 
     for( int i = 0; rdata.mmap_info[i].start != MAP_FAILED; i++ )
@@ -1247,7 +1244,6 @@ excluded_from_wrap (const char *name, char **exclude)
 static int install_wrappers ( void *dl_handle,
                               capsule_item_t *wrappers,
                               const char **exclude,
-                              int dbg,
                               int *errcode,
                               char **error)
 {
@@ -1262,14 +1258,12 @@ static int install_wrappers ( void *dl_handle,
         if( errcode )
             *errcode = EINVAL;
 
-        if( dbg )
-            debug( "mangling dlopen symbols: %s", *error );
+        DEBUG( DEBUG_WRAPPERS, "mangling dlopen symbols: %s", *error );
 
         return -1;
     }
 
-    if ( dbg )
-        debug( "link_map: %p <- %p -> %p",
+    DEBUG( DEBUG_WRAPPERS, "link_map: %p <- %p -> %p",
                map ? map->l_next : NULL ,
                map ? map         : NULL ,
                map ? map->l_prev : NULL );
@@ -1279,12 +1273,12 @@ static int install_wrappers ( void *dl_handle,
     if (map->l_prev)
         for( struct link_map *m = map; m; m = m->l_prev )
             if( !excluded_from_wrap(m->l_name, (char **)exclude) )
-                wrap( m->l_name, m->l_addr, m->l_ld, wrappers, dbg );
+                wrap( m->l_name, m->l_addr, m->l_ld, wrappers );
 
     if (map->l_next)
         for( struct link_map *m = map; m; m = m->l_next )
             if( !excluded_from_wrap(m->l_name, (char **)exclude) )
-                wrap( m->l_name, m->l_addr, m->l_ld, wrappers, dbg );
+                wrap( m->l_name, m->l_addr, m->l_ld, wrappers );
 
     return replacements;
 }
@@ -1294,7 +1288,7 @@ capsule_dlmopen (const char *dso,
                  const char *prefix,
                  Lmid_t *namespace,
                  capsule_item_t *wrappers,
-                 int dbg,
+                 unsigned long dbg,
                  const char **exclude,
                  int *errcode,
                  char **error)
@@ -1302,6 +1296,9 @@ capsule_dlmopen (const char *dso,
     void *ret = NULL;
     ldlibs_t ldlibs = { 0 };
 
+    if( dbg == 0 )
+        dbg = debug_flags;
+
     if( elf_version(EV_CURRENT) == EV_NONE )
     {
         if( error )
@@ -1323,7 +1320,7 @@ capsule_dlmopen (const char *dso,
     // currently installed (x86_64, i386, x32) in no particular order
     if( load_ld_cache( &ldlibs, "/etc/ld.so.cache" ) )
     {
-        if( 0 && dbg )
+        if( debug_flags & DEBUG_LDCACHE )
             dump_ld_cache( &ldlibs );
     }
     else
@@ -1406,7 +1403,7 @@ capsule_dlmopen (const char *dso,
         ldlibs.prefix.len > 0  &&    // have a prefix
         ldlibs.prefix.path     &&
         strcmp("/", ldlibs.prefix.path) ) // prefix is not '/'
-        install_wrappers( ret, wrappers, exclude, 0, errcode, error );
+        install_wrappers( ret, wrappers, exclude, errcode, error );
 
 cleanup:
     cleanup_ldlibs( &ldlibs );
@@ -1425,18 +1422,20 @@ capsule_shim_dlopen(Lmid_t ns,
     char *errors = NULL;
     ldlibs_t ldlibs = { 0 };
 
-    debug( ">>>> capsule dlopen(%s, %x) wrapper: LMID: %ld; prefix: %s;",
+    DEBUG( DEBUG_WRAPPERS,
+           "dlopen(%s, %x) wrapper: LMID: %ld; prefix: %s;",
            file, flag, ns, prefix );
 
     if( prefix && strcmp(prefix, "/") )
     {
-        init_ldlibs( &ldlibs, exclude, prefix, 0, &code, &errors );
+        init_ldlibs( &ldlibs, exclude, prefix, debug_flags, &code, &errors );
 
         if( !load_ld_cache( &ldlibs, "/etc/ld.so.cache" ) )
         {
             int rv = (errno == 0) ? EINVAL : errno;
 
-            debug( "Loading ld.so.cache from %s (error: %d)", prefix, rv );
+            DEBUG( DEBUG_LDCACHE|DEBUG_WRAPPERS,
+                   "Loading ld.so.cache from %s (error: %d)", prefix, rv );
             goto cleanup;
         }
 
@@ -1444,7 +1443,9 @@ capsule_shim_dlopen(Lmid_t ns,
         {
             int rv = (errno == 0) ? EINVAL : errno;
 
-            debug( "<<<< Not found: %s under %s (error: %d)", file, prefix, rv );
+            DEBUG( DEBUG_SEARCH|DEBUG_WRAPPERS,
+                           "Not found: %s under %s (error: %d)",
+                           file, prefix, rv );
             goto cleanup;
         }
 
@@ -1452,7 +1453,7 @@ capsule_shim_dlopen(Lmid_t ns,
 
         if( ldlibs.error )
         {
-            debug( "<<<< capsule dlopen error: %s", ldlibs.error );
+            DEBUG( DEBUG_WRAPPERS, "capsule dlopen error: %s", ldlibs.error );
             goto cleanup;
         }
 
@@ -1460,7 +1461,8 @@ capsule_shim_dlopen(Lmid_t ns,
 
         if( !res )
         {
-            debug( "<<<< capsule dlopen error %d: %s", code, errors );
+            DEBUG( DEBUG_WRAPPERS,
+                   "capsule dlopen error %d: %s", code, errors );
             goto cleanup;
         }
     }
@@ -1469,7 +1471,8 @@ capsule_shim_dlopen(Lmid_t ns,
         res = dlmopen( ns, file, flag );
 
         if( !res )
-            debug( "<<<< capsule dlopen error %s: %s", file, dlerror() );
+            DEBUG( DEBUG_WRAPPERS,
+                   "capsule dlopen error %s: %s", file, dlerror() );
     }
 
     return res;
diff --git a/capsule/capsule-relocate.c b/capsule/capsule-relocate.c
index 9160d945b..de4afb6cb 100644
--- a/capsule/capsule-relocate.c
+++ b/capsule/capsule-relocate.c
@@ -27,8 +27,6 @@
 
 #include <capsule/capsule.h>
 
-#define DEBUG
-
 #include "utils/dump.h"
 #include "utils/utils.h"
 #include "utils/process-pt-dynamic.h"
@@ -73,15 +71,15 @@ relocate_cb (struct dl_phdr_info *info, size_t size, void *data)
 {
     relocation_data_t *rdata = data;
 
-    if( rdata->debug )
-        debug( "processing %s", *info->dlpi_name ? info->dlpi_name : "-elf-" );
+    DEBUG( DEBUG_RELOCS, "processing %s",
+           *info->dlpi_name ? info->dlpi_name : "-elf-" );
 
     return process_phdr( info, size, rdata );
 }
 
 int capsule_relocate (const char *target,
                       void *source,
-                      int dbg,
+                      unsigned long dbg,
                       capsule_item_t *relocations,
                       char **error)
 {
@@ -92,16 +90,18 @@ int capsule_relocate (const char *target,
     int rval = 0;
 
     rdata.target    = target;
-    rdata.debug     = dbg;
+    rdata.debug     = dbg ? dbg : debug_flags;
     rdata.error     = NULL;
     rdata.relocs    = relocations;
     rdata.mmap_info = load_mmap_info( &mmap_errno, &mmap_error );
 
-    if( dbg && (mmap_errno || mmap_error) )
+    if( mmap_errno || mmap_error )
     {
-        debug("mmap/mprotect flags information load error (errno: %d): %s",
-              mmap_errno, mmap_error );
-        debug("relocation will be unable to handle relro linked libraries");
+        DEBUG( DEBUG_RELOCS|DEBUG_MPROTECT,
+               "mmap/mprotect flags information load error (errno: %d): %s\n",
+               mmap_errno, mmap_error );
+        DEBUG( DEBUG_RELOCS|DEBUG_MPROTECT,
+               "relocation will be unable to handle relro linked libraries" );
     }
 
     // no source dl handle means we must have a pre-populated
diff --git a/capsule/capsule.h b/capsule/capsule.h
index fd2071f46..db117b017 100644
--- a/capsule/capsule.h
+++ b/capsule/capsule.h
@@ -30,7 +30,7 @@ void capsule_init (void);
 
 int capsule_relocate (const char *target,
                       void *source,
-                      int debug,
+                      unsigned long debug,
                       capsule_item_t *relocations,
                       char **error);
 
@@ -38,7 +38,7 @@ void *capsule_dlmopen (const char *dso,
                        const char *prefix,
                        Lmid_t *namespace,
                        capsule_item_t *wrappers,
-                       int debug,
+                       unsigned long debug,
                        const char **exclude,
                        int *errcode,
                        char **error);
diff --git a/utils/debug.h b/utils/debug.h
index 9a2ff4b85..b9c53f1b6 100644
--- a/utils/debug.h
+++ b/utils/debug.h
@@ -14,13 +14,13 @@ enum
     DEBUG_ALL        = 0xffff,
 };
 
-#ifdef DEBUG
-#define debug(fmt, args...) \
-    fprintf( stderr, "%s:" fmt "\n", __PRETTY_FUNCTION__, ##args )
-#else
-#define debug(fmt, args...) \
+#define LDLIB_DEBUG(ldl, flags, fmt, args...)  \
+    if( ldl->debug && (ldl->debug & (flags)) ) \
+        fprintf( stderr, "%s:" fmt "\n", __PRETTY_FUNCTION__, ##args )
 
-#endif
+#define DEBUG(flags, fmt, args...)               \
+    if( debug_flags && (debug_flags & (flags)) ) \
+        fprintf( stderr, "%s:" fmt "\n", __PRETTY_FUNCTION__, ##args )
 
 extern unsigned long debug_flags;
 void  set_debug_flags (const char *control);
diff --git a/utils/process-pt-dynamic.c b/utils/process-pt-dynamic.c
index ca8cc33db..3315c18ff 100644
--- a/utils/process-pt-dynamic.c
+++ b/utils/process-pt-dynamic.c
@@ -25,7 +25,6 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
-#define DEBUG
 #include "capsule/capsule.h"
 #include "utils.h"
 #include "process-pt-dynamic.h"
@@ -129,10 +128,6 @@ try_relocation (ElfW(Addr) *reloc_addr, const char *name, void *data)
     capsule_item_t *map;
     relocation_data_t *rdata = data;
 
-    //if( rdata->debug )
-    //    debug( "( %p, %p, %p:%s )",
-    //           rdata, reloc_addr, name, name ? name : "-none-" );
-
     if( !name || !*name || !reloc_addr )
         return 0;
 
@@ -141,13 +136,9 @@ try_relocation (ElfW(Addr) *reloc_addr, const char *name, void *data)
         if( strcmp( name, map->name ) )
             continue;
 
-        if( rdata->debug )
-            debug( "relocation for %s (%p->{ %p }, %p, %p)",
-                   name,
-                   reloc_addr,
-                   NULL,
-                   (void *)map->shim,
-                   (void *)map->real );
+        DEBUG( DEBUG_RELOCS,
+               "relocation for %s (%p->{ %p }, %p, %p)",
+               name, reloc_addr, NULL, (void *)map->shim, (void *)map->real );
 
         // couldn't look up the address of the shim function. buh?
         if( !map->shim )
@@ -157,8 +148,8 @@ try_relocation (ElfW(Addr) *reloc_addr, const char *name, void *data)
         if( !map->real )
         {
             rdata->count.failure++;
-            if( rdata->debug )
-                debug( "--failed");
+            DEBUG( DEBUG_RELOCS, "--failed" );
+
             return 1;
         }
 
@@ -206,14 +197,14 @@ try_relocation (ElfW(Addr) *reloc_addr, const char *name, void *data)
         if( (*reloc_addr == map->shim) &&
             !find_mmap_info(rdata->mmap_info, reloc_addr) )
         {
-            debug( " ERROR: cannot update relocation record for %s", name );
+            DEBUG( DEBUG_RELOCS|DEBUG_MPROTECT,
+                   " ERROR: cannot update relocation record for %s", name );
             return 1; // FIXME - already shimmed, can't seem to override?
         }
 
         *reloc_addr = map->real;
         rdata->count.success++;
-        if( rdata->debug )
-            debug( "--relocated");
+        DEBUG( DEBUG_RELOCS, "--relocated" );
         return 0;
     }
 
@@ -221,7 +212,8 @@ try_relocation (ElfW(Addr) *reloc_addr, const char *name, void *data)
     return 0;
 }
 
-#define DUMP_SLOTINFO(n,x) if(dbg) debug("%s has slot type %s (%d)", n, #x, x)
+#define DUMP_SLOTINFO(n,x) \
+    DEBUG(DEBUG_ELF, "%s has slot type %s (%d)", n, #x, x)
 
 int
 process_dt_rela (const void *start,
@@ -233,8 +225,6 @@ process_dt_rela (const void *start,
 {
     ElfW(Rela) *entry;
 
-    int dbg = ((relocation_data_t *)data)->debug;
-
     for( entry = (ElfW(Rela) *)start;
          entry < (ElfW(Rela) *)(start + relasz);
          entry++ )
@@ -255,17 +245,16 @@ process_dt_rela (const void *start,
             chr = ELF64_R_TYPE(entry->r_info);
             break;
           default:
-            debug( "__ELF_NATIVE_CLASS is neither 32 nor 64" );
-            exit( 1 );
+            fprintf( stderr, "__ELF_NATIVE_CLASS is neither 32 nor 64" );
+            exit( 22 );
         }
 
-        if( dbg )
-            debug( "RELA entry at %p", entry );
+        DEBUG( DEBUG_ELF, "RELA entry at %p", entry );
 
         symbol = find_symbol( sym, symtab, strtab, &name );
 
-        if( dbg )
-            debug( "symbol %p; name: %p:%s", symbol, name, name ? name : "-" );
+        DEBUG( DEBUG_ELF,
+               "symbol %p; name: %p:%s", symbol, name, name ? name : "-" );
 
         if( !symbol || !name || !*name )
             continue;
@@ -277,9 +266,9 @@ process_dt_rela (const void *start,
        // case R_386_JMP_SLOT:  // these are secretly the same:
           case R_X86_64_JUMP_SLOT:
             slot = addr( base, entry->r_offset, entry->r_addend );
-            if( dbg )
-                debug( "R_X86_64_JUMP_SLOT: %p ← { offset: %lu; addend: %ld }",
-                       slot, entry->r_offset, entry->r_addend );
+            DEBUG( DEBUG_ELF,
+                   "R_X86_64_JUMP_SLOT: %p ← { offset: %lu; addend: %ld }",
+                   slot, entry->r_offset, entry->r_addend );
             try_relocation( slot, name, data );
             break;
           case R_X86_64_NONE:
@@ -435,8 +424,8 @@ process_dt_rel (const void *start,
             chr = ELF64_R_TYPE(entry->r_info);
             break;
           default:
-            debug( "__ELF_NATIVE_CLASS is neither 32 nor 64" );
-            exit( 1 );
+            fprintf( stderr, "__ELF_NATIVE_CLASS is neither 32 nor 64" );
+            exit( 22 );
         }
 
         symbol = find_symbol( sym, symtab, strtab, &name );
@@ -477,17 +466,14 @@ process_pt_dynamic (void *start,
     void *relstart;
     const void *symtab = NULL;
     const char *strtab = find_strtab( base, start, size, &strsiz );
-    relocation_data_t *rdata = data;
-    int dbg = rdata->debug;
 
-    if( dbg )
-    {
-        debug( "start: %p; size: %lu; base: %p; handlers: %p %p; …",
-               start, size, (void *)base, process_rela, process_rel );
-        debug( "dyn entry: %p", start + base );
-    }
-    if( dbg )
-        debug( "strtab is at %p: %s%s", strtab, strtab, strtab ? "…" : "");
+    DEBUG( DEBUG_ELF,
+           "start: %p; size: %lu; base: %p; handlers: %p %p; …",
+           start, size, (void *)base, process_rela, process_rel );
+    DEBUG( DEBUG_ELF, "dyn entry: %p", start + base );
+
+    DEBUG( DEBUG_ELF,
+           "strtab is at %p: %s%s", strtab, strtab, strtab ? "…" : "");
 
     for( entry = start + base;
          (entry->d_tag != DT_NULL) && ((void *)entry < (start + base + size));
@@ -496,21 +482,18 @@ process_pt_dynamic (void *start,
         {
           case DT_PLTRELSZ:
             jmprelsz = entry->d_un.d_val;
-            if( dbg )
-                debug( "jmprelsz is %d", jmprelsz );
+            DEBUG( DEBUG_ELF, "jmprelsz is %d", jmprelsz );
             break;
 
           case DT_SYMTAB:
             symtab = addr( base, entry->d_un.d_ptr, 0 );
-            if( dbg )
-                debug( "symtab is %p", symtab );
+            DEBUG( DEBUG_ELF, "symtab is %p", symtab );
             break;
 
           case DT_RELA:
             if( process_rela != NULL )
             {
-                if( dbg )
-                    debug( "processing DT_RELA section" );
+                DEBUG( DEBUG_ELF, "processing DT_RELA section" );
                 if( relasz == -1 )
                     relasz = find_value( base, start, size, DT_RELASZ );
                 relstart = addr( base, entry->d_un.d_ptr, 0 );
@@ -518,23 +501,21 @@ process_pt_dynamic (void *start,
             }
             else
             {
-                if( dbg )
-                    debug( "skipping DT_RELA section: no handler" );
+                DEBUG( DEBUG_ELF|DEBUG_RELOCS,
+                       "skipping DT_RELA section: no handler" );
             }
             break;
 
           case DT_RELASZ:
             relasz = entry->d_un.d_val;
-            if( dbg )
-                debug( "relasz is %d", relasz );
+            DEBUG( DEBUG_ELF, "relasz is %d", relasz );
             break;
 
           case DT_PLTREL:
             jmpreltype = entry->d_un.d_val;
-            if( dbg )
-                debug( "jmpreltype is %d : %s", jmpreltype,
-                       jmpreltype == DT_REL  ? "DT_REL"  :
-                       jmpreltype == DT_RELA ? "DT_RELA" : "???" );
+            DEBUG( DEBUG_ELF, "jmpreltype is %d : %s", jmpreltype,
+                   jmpreltype == DT_REL  ? "DT_REL"  :
+                   jmpreltype == DT_RELA ? "DT_RELA" : "???" );
             break;
 
           case DT_JMPREL:
@@ -548,41 +529,40 @@ process_pt_dynamic (void *start,
               case DT_REL:
                 if( process_rel != NULL )
                 {
-                    if( dbg )
-                        debug( "processing DT_JMPREL/DT_REL section" );
+                    DEBUG( DEBUG_ELF|DEBUG_RELOCS,
+                           "processing DT_JMPREL/DT_REL section" );
                     relstart = addr( base, entry->d_un.d_ptr, 0 );
-                    if( dbg )
-                        debug( "  -> REL antry #0 at %p", relstart );
+                    DEBUG( DEBUG_ELF, "  -> REL antry #0 at %p", relstart );
                     ret = process_rel( relstart, jmprelsz, strtab,
                                        symtab, base, data );
                 }
                 else
                 {
-                    if( dbg )
-                        debug( "skipping DT_JMPREL/DT_REL section: no handler" );
+                    DEBUG( DEBUG_ELF|DEBUG_RELOCS,
+                           "skipping DT_JMPREL/DT_REL section: no handler" );
                 }
                 break;
 
               case DT_RELA:
                 if( process_rela != NULL )
                 {
-                    if( dbg )
-                        debug( "processing DT_JMPREL/DT_RELA section" );
+                    DEBUG( DEBUG_ELF,
+                           "processing DT_JMPREL/DT_RELA section" );
                     relstart = addr( base, entry->d_un.d_ptr, 0 );
                     ret = process_rela( relstart, jmprelsz, strtab,
                                         symtab, base, data );
                 }
                 else
                 {
-                    if( dbg )
-                        debug( "skipping DT_JMPREL/DT_RELA section: no handler" );
+                    DEBUG( DEBUG_ELF,
+                           "skipping DT_JMPREL/DT_RELA section: no handler" );
                 }
                 break;
 
               default:
-                if( dbg )
-                    debug( "Unknown DT_PLTREL value: %d (expected %d or %d)",
-                           jmpreltype, DT_REL, DT_RELA );
+                DEBUG( DEBUG_RELOCS|DEBUG_ELF,
+                       "Unknown DT_PLTREL value: %d (expected %d or %d)",
+                       jmpreltype, DT_REL, DT_RELA );
                 ret = 1;
                 break;
             }
-- 
GitLab