From 8dcce329e2f8e2297fe96520d12f0f7de4f7e08e Mon Sep 17 00:00:00 2001
From: renoseven <dev@renoseven.net>
Date: Mon, 12 May 2025 11:50:09 +0800
Subject: [PATCH] upatch-diff: optimize debug output

Signed-off-by: renoseven <dev@renoseven.net>
---
 upatch-diff/create-diff-object.c |  30 ++------
 upatch-diff/elf-compare.c        |  12 +---
 upatch-diff/elf-correlate.c      |   9 ---
 upatch-diff/elf-create.c         |  38 ----------
 upatch-diff/elf-create.h         |   2 -
 upatch-diff/elf-debug.c          | 120 +++++++++++++------------------
 upatch-diff/elf-debug.h          |   5 +-
 upatch-diff/upatch-elf.c         |  39 ----------
 8 files changed, 60 insertions(+), 195 deletions(-)

diff --git a/upatch-diff/create-diff-object.c b/upatch-diff/create-diff-object.c
index 291542f..c7be45d 100644
--- a/upatch-diff/create-diff-object.c
+++ b/upatch-diff/create-diff-object.c
@@ -522,8 +522,8 @@ static void mark_grouped_sections(struct upatch_elf *uelf)
                 ERROR("Group section not found");
             }
             sec->grouped = true;
-            log_debug("Marking section '%s' (%d) as grouped\n",
-                sec->name, sec->index);
+            log_debug("Marking section grouped, index: %d, name: '%s'\n",
+                sec->index, sec->name);
             data++;
         }
     }
@@ -555,17 +555,12 @@ static void replace_section_syms(struct upatch_elf *uelf)
                 rela->sym->type != STT_SECTION) {
                 continue;
             }
-            log_debug("Found replace symbol for section '%s'\n",
-                rela->sym->name);
-
             /*
              * for section symbol, rela->sym->sec is the section itself.
              * rela->sym->sec->sym is the bundleable symbol which is
              * a function or object.
              */
             if (rela->sym->sec->sym) {
-                log_debug("Act: Replace it with '%s' <- '%s'\n",
-                    rela->sym->sec->sym->name, rela->sym->sec->name);
                 rela->sym = rela->sym->sec->sym;
                 if (rela->sym->sym.st_value != 0) {
                     ERROR("Symbol offset is not zero.");
@@ -601,14 +596,10 @@ static void replace_section_syms(struct upatch_elf *uelf)
                     if (is_mapping_symbol(uelf, sym)) {
                         continue;
                     }
-                    log_debug("Find relocation reference for empty symbol.\n");
                 } else if (target_off < start || target_off >= end) {
                     continue;
                 }
 
-                log_debug("'%s': Replacing '%s+%ld' reference with '%s+%ld'\n",
-                    relasec->name, rela->sym->name, rela->addend,
-                    sym->name, rela->addend - start);
                 found = true;
                 rela->sym = sym;
                 rela->addend -= start;
@@ -680,8 +671,8 @@ static void mark_ignored_sections(struct upatch_elf *uelf)
                 sec->base->name : sec->name;
             if (strncmp(sec_name, ignored_name, name_len) == 0) {
                 sec->ignored = true;
-                log_debug("Marking section '%s' (%d) as ignored\n",
-                    sec->name, sec->index);
+                log_debug("Marking section ignored, index: %d, name: '%s'\n",
+                    sec->index, sec->name);
                 break;
             }
         }
@@ -734,8 +725,6 @@ static void include_symbol(struct symbol *sym)
      * might be needed: either permanently for a rela, or temporarily for
      * the later creation of a dynrela.
      */
-    log_debug("Include symbol '%s', status: %s\n",
-        sym->name, status_str(sym->status));
     sym->include = true;
     /*
      * For special static symbols, we need include it's section
@@ -763,8 +752,6 @@ static void include_section(struct section *sec)
         return;
     }
 
-    log_debug("Include section '%s', status: %s\n",
-        sec->name, status_str(sec->status));
     sec->include = true;
 
     if (is_rela_section(sec)) {
@@ -1021,13 +1008,13 @@ int main(int argc, char **argv)
 
     upatch_correlate_elf(&uelf_source, &uelf_patched);
     upatch_correlate_static_local_variables(&uelf_source, &uelf_patched);
+    upatch_print_correlation(&uelf_patched);
 
     upatch_compare_correlated_elements(&uelf_patched);
     mark_file_symbols(&uelf_source);
     match_local_symbols(&uelf_source, &relf);
 
     include_standard_elements(&uelf_patched);
-
     int change_count = include_changes(&uelf_patched);
     if (change_count == 0) {
         log_normal("No functional changes\n");
@@ -1036,11 +1023,8 @@ int main(int argc, char **argv)
         relf_close(&relf);
         return 0;
     }
-
     upatch_print_changes(&uelf_patched);
 
-    upatch_dump_kelf(&uelf_patched);
-
     verify_patchability(&uelf_patched);
 
     include_special_local_section(&uelf_patched);
@@ -1069,8 +1053,6 @@ int main(int argc, char **argv)
 
     upatch_rebuild_relocations(&uelf_out);
 
-    upatch_check_relocations();
-
     upatch_create_shstrtab(&uelf_out);
 
     upatch_create_strtab(&uelf_out);
@@ -1079,8 +1061,6 @@ int main(int argc, char **argv)
 
     upatch_create_symtab(&uelf_out);
 
-    upatch_dump_kelf(&uelf_out);
-
     upatch_write_output_elf(&uelf_out, uelf_patched.elf, args.output_obj, 0664);
     log_normal("Done\n");
 
diff --git a/upatch-diff/elf-compare.c b/upatch-diff/elf-compare.c
index 227b600..97daa0b 100644
--- a/upatch-diff/elf-compare.c
+++ b/upatch-diff/elf-compare.c
@@ -86,7 +86,6 @@ void upatch_compare_symbols(struct upatch_elf *uelf)
         } else {
             compare_correlated_symbol(sym, sym->twin);
         }
-        log_debug("Symbol '%s' is %s\n", sym->name, status_str(sym->status));
     }
 }
 
@@ -163,20 +162,20 @@ static int compare_correlated_section(struct section *sec, struct section *twin)
 
     if (is_note_section(sec)) {
         sec->status = SAME;
-        goto out;
+        return 0;
     }
     /* As above but for aarch64 */
     if (!strcmp(sec->name, ".rela__patchable_function_entries") ||
         !strcmp(sec->name, "__patchable_function_entries")) {
         sec->status = SAME;
-        goto out;
+        return 0;
     }
     /* compare file size and data size(memory size) */
     if (sec->sh.sh_size != twin->sh.sh_size ||
         sec->data->d_size != twin->data->d_size ||
         (sec->rela && !twin->rela) || (!sec->rela && twin->rela)) {
         sec->status = CHANGED;
-        goto out;
+        return 0;
     }
 
     if (is_rela_section(sec)) {
@@ -185,11 +184,6 @@ static int compare_correlated_section(struct section *sec, struct section *twin)
         compare_correlated_nonrela_section(sec, twin);
     }
 
-out:
-    if (sec->status == CHANGED) {
-        log_debug("section %s has changed\n", sec->name);
-    }
-
     return 0;
 }
 
diff --git a/upatch-diff/elf-correlate.c b/upatch-diff/elf-correlate.c
index 59069a3..d5e76f6 100644
--- a/upatch-diff/elf-correlate.c
+++ b/upatch-diff/elf-correlate.c
@@ -30,9 +30,6 @@
 static void correlate_symbol(struct symbol *sym_orig,
     struct symbol *sym_patched)
 {
-    log_debug("correlate symbol %s <-> %s\n",
-        sym_orig->name, sym_patched->name);
-
     sym_orig->twin = sym_patched;
     sym_patched->twin = sym_orig;
     sym_orig->status = sym_patched->status = SAME;
@@ -109,9 +106,6 @@ void upatch_correlate_symbols(struct upatch_elf *uelf_source,
 static void correlate_section_impl(struct section *sec_orig,
     struct section *sec_patched)
 {
-    log_debug("correlate section %s <-> %s\n",
-        sec_orig->name, sec_patched->name);
-
     sec_orig->twin = sec_patched;
     sec_patched->twin = sec_orig;
     /* set initial status, might change */
@@ -384,15 +378,12 @@ void upatch_correlate_static_local_variables(struct upatch_elf *uelf_source,
             continue;
         }
 
-        log_debug("find normal symbol %s\n", sym->name);
         if (sym->twin) {
             uncorrelate_symbol(sym);
         }
 
         bundled = (sym == sym->sec->sym) ? 1 : 0;
         if (bundled && sym->sec->twin) {
-            log_debug("find bundled static symbol %s\n", sym->name);
-
             uncorrelate_section(sym->sec);
             if (sym->sec->secsym) {
                 uncorrelate_symbol(sym->sec->secsym);
diff --git a/upatch-diff/elf-create.c b/upatch-diff/elf-create.c
index 63dbdb2..964079d 100644
--- a/upatch-diff/elf-create.c
+++ b/upatch-diff/elf-create.c
@@ -384,25 +384,6 @@ void upatch_rebuild_relocations(struct upatch_elf *uelf)
     }
 }
 
-void upatch_check_relocations(void)
-{
-    log_debug("upatch_check_relocations does not work now.\n");
-    return;
-}
-
-static void print_strtab(char *buf, size_t size)
-{
-    size_t i;
-
-    for (i = 0; i < size; i++) {
-        if (buf[i] == 0) {
-            log_debug("\\0");
-        } else {
-            log_debug("%c", buf[i]);
-        }
-    }
-}
-
 void upatch_create_shstrtab(struct upatch_elf *uelf)
 {
     struct section *shstrtab;
@@ -451,17 +432,6 @@ void upatch_create_shstrtab(struct upatch_elf *uelf)
     shstrtab->data->d_buf = buf;
     shstrtab->data->d_size = size;
     shstrtab->dbuf_source = DATA_SOURCE_ALLOC;
-
-    log_debug("shstrtab: ");
-    print_strtab(buf, size);
-    log_debug("\n");
-
-    list_for_each_entry(sec, &uelf->sections, list) {
-        if (sec->ignored) {
-            continue;
-        }
-        log_debug("%s @ shstrtab offset %d\n", sec->name, sec->sh.sh_name);
-    }
 }
 
 void upatch_create_strtab(struct upatch_elf *uelf)
@@ -507,14 +477,6 @@ void upatch_create_strtab(struct upatch_elf *uelf)
     strtab->data->d_buf = buf;
     strtab->data->d_size = size;
     strtab->dbuf_source = DATA_SOURCE_ALLOC;
-
-    log_debug("strtab: ");
-    print_strtab(buf, size);
-    log_debug("\n");
-
-    list_for_each_entry(sym, &uelf->symbols, list) {
-        log_debug("%s @ strtab offset %d\n", sym->name, sym->sym.st_name);
-    }
 }
 
 void upatch_create_symtab(struct upatch_elf *uelf)
diff --git a/upatch-diff/elf-create.h b/upatch-diff/elf-create.h
index f18d40b..18778b5 100644
--- a/upatch-diff/elf-create.h
+++ b/upatch-diff/elf-create.h
@@ -43,8 +43,6 @@ void upatch_reindex_elements(struct upatch_elf *);
 
 void upatch_rebuild_relocations(struct upatch_elf *);
 
-void upatch_check_relocations(void);
-
 void upatch_create_shstrtab(struct upatch_elf *);
 
 void upatch_create_strtab(struct upatch_elf *);
diff --git a/upatch-diff/elf-debug.c b/upatch-diff/elf-debug.c
index 90e112b..44b0e09 100644
--- a/upatch-diff/elf-debug.c
+++ b/upatch-diff/elf-debug.c
@@ -31,17 +31,58 @@
 #include "elf-debug.h"
 #include "upatch-elf.h"
 
+void upatch_print_correlation(struct upatch_elf *uelf)
+{
+    if (uelf == NULL) {
+        return;
+    }
+
+    log_debug("\n------------------------------\n");
+    log_debug("Section\n");
+    log_debug("------------------------------\n");
+    struct section *sec;
+    list_for_each_entry(sec, &uelf->sections, list) {
+        if (sec->twin != NULL) {
+            log_debug("index: %04d, name: '%s' -> index: %04d, name: '%s'\n",
+                sec->index, sec->name, sec->twin->index, sec->twin->name);
+        } else {
+            log_debug("index: %04d, name: '%s' -> None\n",
+                sec->index, sec->name);
+        }
+    }
+    log_debug("------------------------------\n");
+    log_debug("\n");
+    log_debug("------------------------------\n");
+    log_debug("Symbol\n");
+    log_debug("------------------------------\n");
+    struct symbol *sym;
+    list_for_each_entry(sym, &uelf->symbols, list) {
+        if (sym->twin != NULL) {
+            log_debug("index: %04d, name: '%s' -> index: %04d, name: '%s'\n",
+                sym->index, sym->name, sym->twin->index, sym->twin->name);
+        } else {
+            log_debug("index: %04d, name: '%s' -> None\n",
+                sym->index, sym->name);
+        }
+    }
+    log_debug("------------------------------\n");
+}
+
 void upatch_print_changes(struct upatch_elf *uelf)
 {
-    struct symbol *sym = NULL;
-    struct section *sec = NULL;
+    struct symbol *sym;
+    struct section *sec;
 
-    log_normal("------------------------------\n");
+    if (uelf == NULL) {
+        return;
+    }
+
+    log_normal("\n------------------------------\n");
     log_normal("New symbol\n");
     log_normal("------------------------------\n");
     list_for_each_entry(sym, &uelf->symbols, list) {
         if (sym->status == NEW) {
-            log_normal("idx: %04u, name: '%s'\n", sym->index, sym->name);
+            log_normal("index: %04d, name: '%s'\n", sym->index, sym->name);
         }
     }
     log_normal("------------------------------\n");
@@ -51,7 +92,7 @@ void upatch_print_changes(struct upatch_elf *uelf)
     log_normal("------------------------------\n");
     list_for_each_entry(sec, &uelf->sections, list) {
         if (sec->status == NEW) {
-            log_normal("idx: %04u, name: '%s'\n", sec->index, sec->name);
+            log_normal("index: %04d, name: '%s'\n", sec->index, sec->name);
         }
     }
     log_normal("------------------------------\n");
@@ -61,7 +102,7 @@ void upatch_print_changes(struct upatch_elf *uelf)
     log_normal("------------------------------\n");
     list_for_each_entry(sym, &uelf->symbols, list) {
         if (sym->status == CHANGED) {
-            log_normal("idx: %04u, name: '%s'\n", sym->index, sym->name);
+            log_normal("index: %04d, name: '%s'\n", sym->index, sym->name);
         }
     }
     log_normal("------------------------------\n");
@@ -71,7 +112,7 @@ void upatch_print_changes(struct upatch_elf *uelf)
     log_normal("------------------------------\n");
     list_for_each_entry(sec, &uelf->sections, list) {
         if (sec->status == CHANGED) {
-            log_normal("idx: %04u, name: '%s'\n", sec->index, sec->name);
+            log_normal("index: %04d, name: '%s'\n", sec->index, sec->name);
         }
     }
     log_normal("------------------------------\n");
@@ -81,7 +122,7 @@ void upatch_print_changes(struct upatch_elf *uelf)
     log_normal("------------------------------\n");
     list_for_each_entry(sym, &uelf->symbols, list) {
         if (sym->include) {
-            log_normal("idx: %04u, name: '%s', status: %s\n",
+            log_normal("index: %04d, name: '%s', status: %s\n",
                 sym->index, sym->name, status_str(sym->status));
         }
     }
@@ -92,70 +133,9 @@ void upatch_print_changes(struct upatch_elf *uelf)
     log_normal("------------------------------\n");
     list_for_each_entry(sec, &uelf->sections, list) {
         if (sec->include) {
-            log_normal("idx: %04u, name: '%s', status: %s\n",
+            log_normal("index: %04d, name: '%s', status: %s\n",
                 sec->index, sec->name, status_str(sec->status));
         }
     }
     log_normal("------------------------------\n");
 }
-
-void upatch_dump_kelf(struct upatch_elf *uelf)
-{
-    struct section *sec;
-    struct symbol *sym;
-    struct rela *rela;
-
-    log_debug("\n=== Sections ===\n");
-    list_for_each_entry(sec, &uelf->sections, list) {
-        log_debug("%02d %s (%s)",
-            sec->index, sec->name, status_str(sec->status));
-        if (is_rela_section(sec)) {
-            if (sec->ignored) {
-                continue;
-            }
-            log_debug(", base-> %s\n", sec->base->name);
-            log_debug("rela section expansion\n");
-            list_for_each_entry(rela, &sec->relas, list) {
-                log_debug("sym %d, offset %ld, type %d, %s %s %ld\n",
-                    rela->sym->index, rela->offset,
-                    rela->type, rela->sym->name,
-                    (rela->addend < 0) ? "-" : "+",
-                    labs(rela->addend));
-            }
-        } else {
-            if (sec->sym) {
-                log_debug(", sym-> %s", sec->sym->name);
-            }
-            if (sec->secsym) {
-                log_debug(", secsym-> %s", sec->secsym->name);
-            }
-            if (sec->rela) {
-                log_debug(", rela-> %s", sec->rela->name);
-            }
-        }
-        log_debug("\n");
-    }
-
-    log_debug("\n=== Symbols ===\n");
-    list_for_each_entry(sym, &uelf->symbols, list) {
-        log_debug("sym %02d, type %d, bind %d, ndx %02d, name %s (%s)",
-            sym->index, sym->type, sym->bind, sym->sym.st_shndx,
-            sym->name, status_str(sym->status));
-        if (sym->sec && (sym->type == STT_FUNC || sym->type == STT_OBJECT)) {
-            log_debug(" -> %s", sym->sec->name);
-        }
-        log_debug("\n");
-    }
-}
-
-/* debuginfo releated */
-static inline bool skip_bytes(unsigned char **iter, unsigned char *end,
-    unsigned int len)
-{
-    if ((unsigned int)(end - *iter) < len) {
-        *iter = end;
-        return false;
-    }
-    *iter += len;
-    return true;
-}
diff --git a/upatch-diff/elf-debug.h b/upatch-diff/elf-debug.h
index 7c8db40..09122f6 100644
--- a/upatch-diff/elf-debug.h
+++ b/upatch-diff/elf-debug.h
@@ -27,8 +27,7 @@
 
 #include "upatch-elf.h"
 
-void upatch_print_changes(struct upatch_elf *);
-
-void upatch_dump_kelf(struct upatch_elf *);
+void upatch_print_correlation(struct upatch_elf *uelf);
+void upatch_print_changes(struct upatch_elf *uelf);
 
 #endif /* __UPATCH_ELF_DEBUG_H_ */
diff --git a/upatch-diff/upatch-elf.c b/upatch-diff/upatch-elf.c
index 2f88a92..81567f5 100644
--- a/upatch-diff/upatch-elf.c
+++ b/upatch-diff/upatch-elf.c
@@ -56,7 +56,6 @@ static void create_section_list(struct upatch_elf *uelf)
         ERROR("elf_getshdrstrndx with error %s", elf_errmsg(0));
     }
 
-    log_debug("=== section list (%zu) ===\n", sections_nr);
     while (sections_nr--) {
         ALLOC_LINK(sec, &uelf->sections);
 
@@ -87,9 +86,6 @@ static void create_section_list(struct upatch_elf *uelf)
         if (sec->sh.sh_type == SHT_SYMTAB_SHNDX) {
             uelf->symtab_shndx = sec->data; /* correct ? */
         }
-
-        log_debug("ndx %02d, data %p, size %zu, name %s\n",
-            sec->index, sec->data->d_buf, sec->data->d_size, sec->name);
     }
 
     if (elf_nextscn(uelf->elf, scn)) {
@@ -114,7 +110,6 @@ static void create_symbol_list(struct upatch_elf *uelf)
 
     symbols_nr = (unsigned int)(symtab->sh.sh_size / symtab->sh.sh_entsize);
 
-    log_debug("\n=== symbol list (%d entries) ===\n", symbols_nr);
     while (symbols_nr--) {
         ALLOC_LINK(sym, &uelf->symbols);
         INIT_LIST_HEAD(&sym->children);
@@ -157,12 +152,6 @@ static void create_symbol_list(struct upatch_elf *uelf)
                 sym->name = sym->sec->name;
             }
         }
-        log_debug("sym %02d, type %d, bind %d, ndx %02d, name %s",
-            sym->index, sym->type, sym->bind, sym->sym.st_shndx, sym->name);
-        if (sym->sec) {
-            log_debug(" -> %s", sym->sec->name);
-        }
-        log_debug("\n");
     }
 }
 
@@ -173,7 +162,6 @@ static void create_rela_list(struct upatch_elf *uelf, struct section *relasec)
 
     unsigned int symndx;
     int index = 0;
-    int skip = 0;
 
     INIT_LIST_HEAD(&relasec->relas);
 
@@ -182,23 +170,9 @@ static void create_rela_list(struct upatch_elf *uelf, struct section *relasec)
     if (!relasec->base) {
         ERROR("no base section found for relocation section %s", relasec->name);
     }
-
     relasec->base->rela = relasec;
     rela_nr = relasec->sh.sh_size / relasec->sh.sh_entsize;
 
-    log_debug("\n=== rela list for %s (%ld entries) ===\n",
-        relasec->base->name, rela_nr);
-
-    if (is_debug_section(relasec)) {
-        log_debug("skipping rela listing for .debug_* section\n");
-        skip = 1;
-    }
-
-    if (is_note_section(relasec)) {
-        log_debug("skipping rela listing for .note_* section\n");
-        skip = 1;
-    }
-
     while (rela_nr--) {
         ALLOC_LINK(rela, &relasec->relas);
 
@@ -226,19 +200,6 @@ static void create_rela_list(struct upatch_elf *uelf, struct section *relasec)
                     rela->sym->name, rela->addend);
             }
         }
-
-        if (skip) {
-            continue;
-        }
-
-        log_debug("offset %ld, type %d, %s %s %ld", rela->offset,
-            rela->type, rela->sym->name,
-            (rela->addend < 0) ? "-" : "+", labs(rela->addend));
-        if (rela->string)  {
-            // rela->string is not utf8
-            log_debug(" string = %s", rela->string);
-        }
-        log_debug("\n");
     }
 }
 
-- 
2.43.0