Skip to content

Commit 6ca9f9b

Browse files
committed
Added base address if needed.
Ranges of locations are relative to some base address. Most times this is just the same as the compilation unit. However if the compilation unit contains functions in multiple sections we need to add a base address of the section that the locations are contained.
1 parent d5435a3 commit 6ca9f9b

File tree

3 files changed

+53
-33
lines changed

3 files changed

+53
-33
lines changed

debug/DwarfPrinter.ml

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,13 @@ module DwarfPrinter(Target: DWARF_TARGET):
602602
print_sleb128 oc "" 0;
603603
print_label oc debug_end (* End of the debug section *)
604604

605-
let print_location_entry oc c_low l =
605+
let print_location_entry oc needs_base c_low l =
606606
print_label oc (loc_to_label l.loc_id);
607+
(* If we have multiple ranges per compilation unit we need to specify a base address for the location *)
608+
if needs_base then begin
609+
fprintf oc " %s -1\n" address;
610+
fprintf oc " %s %a\n" address label c_low;
611+
end;
607612
List.iter (fun (b,e,loc) ->
608613
fprintf oc " %s %a-%a\n" address label b label c_low;
609614
fprintf oc " %s %a-%a\n" address label e label c_low;
@@ -621,11 +626,11 @@ module DwarfPrinter(Target: DWARF_TARGET):
621626
fprintf oc " %s 0\n" address
622627

623628

624-
let print_location_list oc (c_low,l) =
625-
let f = match c_low with
626-
| Some s -> print_location_entry oc s
627-
| None -> print_location_entry_abs oc in
628-
List.iter f l
629+
let print_location_list oc needs_base l =
630+
let f l = match l.loc_sec_begin with
631+
| Some s -> print_location_entry oc needs_base s l
632+
| None -> print_location_entry_abs oc l in
633+
List.iter f l
629634

630635
let list_opt l f =
631636
match l with
@@ -635,15 +640,15 @@ module DwarfPrinter(Target: DWARF_TARGET):
635640
let print_diab_entries oc entries =
636641
let abbrev_start = new_label () in
637642
abbrev_start_addr := abbrev_start;
638-
List.iter (fun e -> compute_abbrev e.entry) entries;
643+
List.iter (fun e -> compute_abbrev e.diab_entry) entries;
639644
print_abbrev oc;
640645
List.iter (fun e ->
641646
let name = if e.section_name <> ".text" then Some e.section_name else None in
642647
section oc (Section_debug_info name);
643-
print_debug_info oc e.start_label e.line_label e.entry) entries;
644-
if List.exists (fun e -> match e.dlocs with _,[] -> false | _,_ -> true) entries then begin
648+
print_debug_info oc e.start_label e.line_label e.diab_entry) entries;
649+
if List.exists (fun e -> match e.diab_locs with [] -> false | _ -> true) entries then begin
645650
section oc Section_debug_loc;
646-
List.iter (fun e -> print_location_list oc e.dlocs) entries
651+
List.iter (fun e -> print_location_list oc false e.diab_locs) entries
647652
end
648653

649654
let print_ranges oc r =
@@ -665,27 +670,27 @@ module DwarfPrinter(Target: DWARF_TARGET):
665670
fprintf oc " %s 0\n" address;
666671
fprintf oc " %s 0\n" address) r
667672

668-
let print_gnu_entries oc cp (lpc,loc) s r =
669-
compute_abbrev cp;
673+
let print_gnu_entries oc entries =
674+
compute_abbrev entries.gnu_entry;
670675
let line_start = new_label ()
671676
and start = new_label ()
672677
and abbrev_start = new_label ()
673678
and range_label = new_label () in
674679
debug_ranges_addr := range_label;
675680
abbrev_start_addr := abbrev_start;
676681
section oc (Section_debug_info None);
677-
print_debug_info oc start line_start cp;
682+
print_debug_info oc start line_start entries.gnu_entry;
678683
print_abbrev oc;
679-
list_opt loc (fun () ->
684+
list_opt entries.gnu_locs (fun () ->
680685
section oc Section_debug_loc;
681-
print_location_list oc (lpc,loc));
682-
list_opt r (fun () ->
683-
print_ranges oc r);
686+
print_location_list oc entries.several_secs entries.gnu_locs);
687+
list_opt entries.range_table (fun () ->
688+
print_ranges oc entries.range_table);
684689
section oc (Section_debug_line None);
685690
print_label oc line_start;
686-
list_opt s (fun () ->
691+
list_opt entries.string_table (fun () ->
687692
section oc Section_debug_str;
688-
let s = List.sort (fun (a,_) (b,_) -> compare a b) s in
693+
let s = List.sort (fun (a,_) (b,_) -> compare a b) entries.string_table in
689694
List.iter (fun (id,s) ->
690695
print_label oc (loc_to_label id);
691696
fprintf oc " .asciz %S\n" s) s)
@@ -698,6 +703,6 @@ module DwarfPrinter(Target: DWARF_TARGET):
698703
Hashtbl.clear loc_labels;
699704
match debug with
700705
| Diab entries -> print_diab_entries oc entries
701-
| Gnu (cp,loc,s,r) -> print_gnu_entries oc cp loc s r
706+
| Gnu entries -> print_gnu_entries oc entries
702707

703708
end

debug/DwarfTypes.mli

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,13 @@ type dw_entry =
266266

267267
(* The type for the location list. *)
268268
type location_entry =
269-
{
270-
loc: (address * address * location_value) list;
271-
loc_id: reference;
272-
}
273-
type dw_locations = constant option * location_entry list
269+
{
270+
loc: (address * address * location_value) list;
271+
loc_id: reference;
272+
loc_sec_begin : address option;
273+
}
274+
275+
type dw_locations = location_entry list
274276

275277
type range_entry =
276278
| AddressRange of (address * address) list
@@ -285,13 +287,20 @@ type diab_entry =
285287
section_name: string;
286288
start_label: int;
287289
line_label: int;
288-
entry: dw_entry;
289-
dlocs: dw_locations;
290+
diab_entry: dw_entry;
291+
diab_locs: dw_locations;
290292
}
291293

292294
type diab_entries = diab_entry list
293295

294-
type gnu_entries = dw_entry * dw_locations * dw_string * dw_ranges
296+
type gnu_entries =
297+
{
298+
string_table: dw_string;
299+
range_table: dw_ranges;
300+
gnu_locs: dw_locations;
301+
gnu_entry: dw_entry;
302+
several_secs: bool;
303+
}
295304

296305
type debug_entries =
297306
| Diab of diab_entries

debug/Dwarfgen.ml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ module Dwarfgenaux (Target: TARGET) =
408408
and lo = translate_label f_id lo in
409409
hi,lo,range_entry_loc i.var_loc) l in
410410
let id = next_id () in
411-
Some (LocRef id),[{loc = l;loc_id = id;}]
411+
Some (LocRef id),[{loc_sec_begin = !current_section_start; loc = l;loc_id = id;}]
412412
end
413413
with Not_found -> None,[]
414414
else
@@ -574,8 +574,8 @@ let diab_gen_compilation_section sec_name s defs acc =
574574
section_name = s;
575575
start_label = debug_start;
576576
line_label = line_start;
577-
entry = cp;
578-
dlocs = Some low_pc,accu.locs;
577+
diab_entry = cp;
578+
diab_locs = accu.locs;
579579
}::acc
580580

581581
let gen_diab_debug_info sec_name var_section : debug_entries =
@@ -643,6 +643,12 @@ let gen_gnu_debug_info sec_name var_section : debug_entries =
643643
} in
644644
let cp = new_entry (next_id ()) (DW_TAG_compile_unit cp) in
645645
let cp = add_children cp (types@defs) in
646-
let loc_pc = if StringSet.cardinal sec > 1 then None else low_pc in
647646
let string_table = Hashtbl.fold (fun s i acc -> (i,s)::acc) string_table [] in
648-
Gnu (cp,(loc_pc,accu.locs),string_table,snd accu.ranges)
647+
let cp = {
648+
string_table = string_table;
649+
range_table = snd accu.ranges;
650+
gnu_locs = accu.locs;
651+
gnu_entry = cp;
652+
several_secs = StringSet.cardinal sec > 1}
653+
in
654+
Gnu cp

0 commit comments

Comments
 (0)