b73d7b
From 3b91792d3d644d6d6b0059cb315c9fe5d3626bab Mon Sep 17 00:00:00 2001
b73d7b
From: Yusuke Endoh <mame@ruby-lang.org>
b73d7b
Date: Sat, 6 Mar 2021 00:03:57 +0900
b73d7b
Subject: [PATCH] Support GCC's DWARF 5 [Bug #17585]
b73d7b
b73d7b
Co-Authored-By: xtkoba (Tee KOBAYASHI) <xtkoba+ruby@gmail.com>
b73d7b
---
b73d7b
 addr2line.c | 119 ++++++++++++++++++++++++++++++++++++++++++----------
b73d7b
 1 file changed, 97 insertions(+), 22 deletions(-)
b73d7b
b73d7b
diff --git a/addr2line.c b/addr2line.c
b73d7b
index 0029cffbca..855efb40d4 100644
4da821
--- a/addr2line.c
4da821
+++ b/addr2line.c
b73d7b
@@ -159,11 +159,12 @@ typedef struct obj_info {
4da821
     struct dwarf_section debug_info;
4da821
     struct dwarf_section debug_line;
4da821
     struct dwarf_section debug_ranges;
4da821
+    struct dwarf_section debug_rnglists;
4da821
     struct dwarf_section debug_str;
4da821
     struct obj_info *next;
4da821
 } obj_info_t;
4da821
 
4da821
-#define DWARF_SECTION_COUNT 5
4da821
+#define DWARF_SECTION_COUNT 6
4da821
 
4da821
 static struct dwarf_section *
4da821
 obj_dwarf_section_at(obj_info_t *obj, int n)
b73d7b
@@ -173,6 +174,7 @@ obj_dwarf_section_at(obj_info_t *obj, int n)
4da821
         &obj->debug_info,
4da821
         &obj->debug_line,
4da821
         &obj->debug_ranges,
4da821
+        &obj->debug_rnglists,
4da821
         &obj->debug_str
4da821
     };
4da821
     if (n < 0 || DWARF_SECTION_COUNT <= n) {
b73d7b
@@ -411,7 +413,7 @@ parse_debug_line_cu(int num_traces, void **traces, char **debug_line,
4da821
 	    FILL_LINE();
4da821
 	    break;
4da821
 	case DW_LNS_advance_pc:
4da821
-	    a = uleb128((char **)&p);
4da821
+	    a = uleb128((char **)&p) * header.minimum_instruction_length;
4da821
 	    addr += a;
4da821
 	    break;
4da821
 	case DW_LNS_advance_line: {
b73d7b
@@ -451,7 +453,7 @@ parse_debug_line_cu(int num_traces, void **traces, char **debug_line,
4da821
 	    /* isa = (unsigned int)*/(void)uleb128((char **)&p);
4da821
 	    break;
4da821
 	case 0:
4da821
-	    a = *(unsigned char *)p++;
4da821
+	    a = uleb128((char **)&p);
4da821
 	    op = *p++;
4da821
 	    switch (op) {
4da821
 	    case DW_LNE_end_sequence:
b73d7b
@@ -808,6 +810,18 @@ enum
4da821
     DW_FORM_addrx4 = 0x2c
4da821
 };
4da821
 
4da821
+/* Range list entry encodings */
4da821
+enum {
4da821
+    DW_RLE_end_of_list = 0x00,
4da821
+    DW_RLE_base_addressx = 0x01,
4da821
+    DW_RLE_startx_endx = 0x02,
4da821
+    DW_RLE_startx_length = 0x03,
4da821
+    DW_RLE_offset_pair = 0x04,
4da821
+    DW_RLE_base_address = 0x05,
4da821
+    DW_RLE_start_end = 0x06,
4da821
+    DW_RLE_start_length = 0x07
4da821
+};
4da821
+
4da821
 enum {
4da821
     VAL_none = 0,
4da821
     VAL_cstr = 1,
b73d7b
@@ -961,6 +975,23 @@ debug_info_reader_init(DebugInfoReader *reader, obj_info_t *obj)
b73d7b
     reader->current_low_pc = 0;
4da821
 }
4da821
 
b73d7b
+static void
4da821
+di_skip_die_attributes(char **p)
4da821
+{
4da821
+    for (;;) {
4da821
+        uint64_t at = uleb128(p);
4da821
+        uint64_t form = uleb128(p);
4da821
+        if (!at && !form) break;
4da821
+        switch (form) {
4da821
+          default:
4da821
+            break;
4da821
+          case DW_FORM_implicit_const:
4da821
+            sleb128(p);
4da821
+            break;
4da821
+        }
4da821
+    }
4da821
+}
4da821
+
b73d7b
 static void
4da821
 di_read_debug_abbrev_cu(DebugInfoReader *reader)
4da821
 {
b73d7b
@@ -975,12 +1006,7 @@ di_read_debug_abbrev_cu(DebugInfoReader *reader)
4da821
         prev = abbrev_number;
4da821
         uleb128(&p); /* tag */
4da821
         p++; /* has_children */
4da821
-        /* skip content */
4da821
-        for (;;) {
4da821
-            uint64_t at = uleb128(&p);
4da821
-            uint64_t form = uleb128(&p);
4da821
-            if (!at && !form) break;
4da821
-        }
4da821
+        di_skip_die_attributes(&p);
4da821
     }
4da821
 }
4da821
 
b73d7b
@@ -1244,12 +1270,7 @@ di_find_abbrev(DebugInfoReader *reader, uint64_t abbrev_number)
4da821
     /* skip 255th record */
4da821
     uleb128(&p); /* tag */
4da821
     p++; /* has_children */
4da821
-    /* skip content */
4da821
-    for (;;) {
4da821
-        uint64_t at = uleb128(&p);
4da821
-        uint64_t form = uleb128(&p);
4da821
-        if (!at && !form) break;
4da821
-    }
4da821
+    di_skip_die_attributes(&p);
4da821
     for (uint64_t n = uleb128(&p); abbrev_number != n; n = uleb128(&p)) {
4da821
         if (n == 0) {
4da821
             fprintf(stderr,"%d: Abbrev Number %"PRId64" not found\n",__LINE__, abbrev_number);
b73d7b
@@ -1257,12 +1278,7 @@ di_find_abbrev(DebugInfoReader *reader, uint64_t abbrev_number)
4da821
         }
4da821
         uleb128(&p); /* tag */
4da821
         p++; /* has_children */
4da821
-        /* skip content */
4da821
-        for (;;) {
4da821
-            uint64_t at = uleb128(&p);
4da821
-            uint64_t form = uleb128(&p);
4da821
-            if (!at && !form) break;
4da821
-        }
4da821
+        di_skip_die_attributes(&p);
4da821
     }
4da821
     return p;
4da821
 }
b73d7b
@@ -1390,6 +1406,21 @@ ranges_set(ranges_t *ptr, DebugInfoValue *v)
4da821
     }
4da821
 }
4da821
 
4da821
+static uint64_t
4da821
+read_dw_form_addr(DebugInfoReader *reader, char **ptr)
4da821
+{
4da821
+    char *p = *ptr;
4da821
+    *ptr = p + reader->format;
4da821
+    if (reader->format == 4) {
4da821
+        return read_uint32(&p);
4da821
+    } else if (reader->format == 8) {
4da821
+        return read_uint64(&p);
4da821
+    } else {
4da821
+        fprintf(stderr,"unknown address_size:%d", reader->address_size);
4da821
+        abort();
4da821
+    }
4da821
+}
4da821
+
4da821
 static uintptr_t
4da821
 ranges_include(DebugInfoReader *reader, ranges_t *ptr, uint64_t addr)
4da821
 {
b73d7b
@@ -1403,8 +1434,50 @@ ranges_include(DebugInfoReader *reader, ranges_t *ptr, uint64_t addr)
4da821
     }
4da821
     else if (ptr->ranges_set) {
4da821
         /* TODO: support base address selection entry */
4da821
-        char *p = reader->obj->debug_ranges.ptr + ptr->ranges;
4da821
+        char *p;
4da821
         uint64_t base = ptr->low_pc_set ? ptr->low_pc : reader->current_low_pc;
4da821
+        if (reader->obj->debug_rnglists.ptr) {
4da821
+            p = reader->obj->debug_rnglists.ptr + ptr->ranges;
4da821
+            for (;;) {
4da821
+                uint8_t rle = read_uint8(&p);
4da821
+                uintptr_t base_address = 0;
4da821
+                uintptr_t from, to;
4da821
+                if (rle == DW_RLE_end_of_list) break;
4da821
+                switch (rle) {
4da821
+                  case DW_RLE_base_addressx:
4da821
+                    uleb128(&p);
4da821
+                    break;
4da821
+                  case DW_RLE_startx_endx:
4da821
+                    uleb128(&p);
4da821
+                    uleb128(&p);
4da821
+                    break;
4da821
+                  case DW_RLE_startx_length:
4da821
+                    uleb128(&p);
4da821
+                    uleb128(&p);
4da821
+                    break;
4da821
+                  case DW_RLE_offset_pair:
4da821
+                    from = base_address + uleb128(&p);
4da821
+                    to = base_address + uleb128(&p);
4da821
+                    if (base + from <= addr && addr < base + to) {
4da821
+                        return from;
4da821
+                    }
4da821
+                    break;
4da821
+                  case DW_RLE_base_address:
4da821
+                    base_address = read_dw_form_addr(reader, &p);
4da821
+                    break;
4da821
+                  case DW_RLE_start_end:
4da821
+                    read_dw_form_addr(reader, &p);
4da821
+                    read_dw_form_addr(reader, &p);
4da821
+                    break;
4da821
+                  case DW_RLE_start_length:
4da821
+                    read_dw_form_addr(reader, &p);
4da821
+                    uleb128(&p);
4da821
+                    break;
4da821
+                }
4da821
+            }
4da821
+            return false;
4da821
+        }
4da821
+        p = reader->obj->debug_ranges.ptr + ptr->ranges;
4da821
         for (;;) {
4da821
             uintptr_t from = read_uintptr(&p);
4da821
             uintptr_t to = read_uintptr(&p);
b73d7b
@@ -1750,6 +1823,7 @@ fill_lines(int num_traces, void **traces, int check_debuglink,
4da821
                     ".debug_info",
4da821
                     ".debug_line",
4da821
                     ".debug_ranges",
4da821
+                    ".debug_rnglists",
4da821
                     ".debug_str"
4da821
                 };
4da821
 
b73d7b
@@ -2006,6 +2080,7 @@ found_mach_header:
4da821
                     "__debug_info",
4da821
                     "__debug_line",
4da821
                     "__debug_ranges",
4da821
+                    "__debug_rnglists",
4da821
                     "__debug_str"
4da821
                 };
4da821
                 struct LP(segment_command) *scmd = (struct LP(segment_command) *)lcmd;