@@ -2,6 +2,7 @@ use super::mystd::ffi::{OsStr, OsString};
22use super :: mystd:: fs;
33use super :: mystd:: os:: unix:: ffi:: { OsStrExt , OsStringExt } ;
44use super :: mystd:: path:: { Path , PathBuf } ;
5+ use super :: Either ;
56use super :: { Context , Mapping , Stash , Vec } ;
67use core:: convert:: { TryFrom , TryInto } ;
78use core:: str;
@@ -18,75 +19,50 @@ type Elf = object::elf::FileHeader64<NativeEndian>;
1819impl Mapping {
1920 pub fn new ( path : & Path ) -> Option < Mapping > {
2021 let map = super :: mmap ( path) ?;
21- let object = Object :: parse ( & map) ?;
22+ Mapping :: mk_or_other ( map, |map, stash| {
23+ let object = Object :: parse ( & map) ?;
2224
23- // Try to locate an external debug file using the build ID.
24- if let Some ( path_debug) = object. build_id ( ) . and_then ( locate_build_id) {
25- if let Some ( mapping) = Mapping :: new_debug ( path_debug, None ) {
26- return Some ( mapping) ;
25+ // Try to locate an external debug file using the build ID.
26+ if let Some ( path_debug) = object. build_id ( ) . and_then ( locate_build_id) {
27+ if let Some ( mapping) = Mapping :: new_debug ( path_debug, None ) {
28+ return Some ( Either :: A ( mapping) ) ;
29+ }
2730 }
28- }
2931
30- // Try to locate an external debug file using the GNU debug link section.
31- if let Some ( ( path_debug, crc) ) = object. gnu_debuglink_path ( path) {
32- if let Some ( mapping) = Mapping :: new_debug ( path_debug, Some ( crc) ) {
33- return Some ( mapping) ;
32+ // Try to locate an external debug file using the GNU debug link section.
33+ if let Some ( ( path_debug, crc) ) = object. gnu_debuglink_path ( path) {
34+ if let Some ( mapping) = Mapping :: new_debug ( path_debug, Some ( crc) ) {
35+ return Some ( Either :: A ( mapping) ) ;
36+ }
3437 }
35- }
3638
37- let stash = Stash :: new ( ) ;
38- let cx = Context :: new ( & stash, object, None ) ?;
39- Some ( Mapping {
40- // Convert to 'static lifetimes since the symbols should
41- // only borrow `map` and `stash` and we're preserving them below.
42- cx : unsafe { core:: mem:: transmute :: < Context < ' _ > , Context < ' static > > ( cx) } ,
43- _map : map,
44- _map_sup : None ,
45- _stash : stash,
39+ Context :: new ( stash, object, None ) . map ( Either :: B )
4640 } )
4741 }
4842
4943 /// Load debuginfo from an external debug file.
5044 fn new_debug ( path : PathBuf , crc : Option < u32 > ) -> Option < Mapping > {
5145 let map = super :: mmap ( & path) ?;
52- let object = Object :: parse ( & map) ?;
46+ Mapping :: mk ( map, |map, stash| {
47+ let object = Object :: parse ( & map) ?;
5348
54- if let Some ( _crc) = crc {
55- // TODO: check crc
56- }
49+ if let Some ( _crc) = crc {
50+ // TODO: check crc
51+ }
5752
58- // Try to locate a supplementary object file.
59- if let Some ( ( path_sup, build_id_sup) ) = object. gnu_debugaltlink_path ( & path) {
60- if let Some ( map_sup) = super :: mmap ( & path_sup) {
61- if let Some ( sup) = Object :: parse ( & map_sup) {
62- if sup. build_id ( ) == Some ( build_id_sup) {
63- let stash = Stash :: new ( ) ;
64- let cx = Context :: new ( & stash, object, Some ( sup) ) ?;
65- return Some ( Mapping {
66- // Convert to 'static lifetimes since the symbols should
67- // only borrow `map`, `map_sup`, and `stash` and we're
68- // preserving them below.
69- cx : unsafe {
70- core:: mem:: transmute :: < Context < ' _ > , Context < ' static > > ( cx)
71- } ,
72- _map : map,
73- _map_sup : Some ( map_sup) ,
74- _stash : stash,
75- } ) ;
53+ // Try to locate a supplementary object file.
54+ if let Some ( ( path_sup, build_id_sup) ) = object. gnu_debugaltlink_path ( & path) {
55+ if let Some ( map_sup) = super :: mmap ( & path_sup) {
56+ let map_sup = stash. set_mmap_aux ( map_sup) ;
57+ if let Some ( sup) = Object :: parse ( map_sup) {
58+ if sup. build_id ( ) == Some ( build_id_sup) {
59+ return Context :: new ( stash, object, Some ( sup) ) ;
60+ }
7661 }
7762 }
7863 }
79- }
8064
81- let stash = Stash :: new ( ) ;
82- let cx = Context :: new ( & stash, object, None ) ?;
83- Some ( Mapping {
84- // Convert to 'static lifetimes since the symbols should
85- // only borrow `map` and `stash` and we're preserving them below.
86- cx : unsafe { core:: mem:: transmute :: < Context < ' _ > , Context < ' static > > ( cx) } ,
87- _map : map,
88- _map_sup : None ,
89- _stash : stash,
65+ Context :: new ( stash, object, None )
9066 } )
9167 }
9268}
0 commit comments