Skip to content

Commit 05ced1b

Browse files
authored
prunes empty segments (#1281)
Filters out empty segments from the specification. An empty segment breaks, in particular, the primus loader which treats it as a segment that covers all addresses (an appropriate assert is added to make it easier to discover such issues in the future).
1 parent 4454b5d commit 05ced1b

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

lib/bap_llvm/bap_llvm_loader.ml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,8 @@ let provide_macho_segments =
194194
$ LLVM.segment_cmd_flags
195195
$ LLVM.virtual_segment_cmd) @@
196196
fun (name, off, size) (_,(r,w,x)) (_,addr,vsize) ->
197-
let addr = Int64.(bias + addr) in [
197+
let addr = Int64.(bias + addr) in
198+
provide_if Int64.(size <> 0L) [
198199
Ogre.provide segment addr vsize r w x;
199200
Ogre.provide named_region addr vsize name;
200201
Ogre.provide mapped addr size off;
@@ -228,7 +229,8 @@ let map_sections_to_segments =
228229
LLVM.section_entry
229230
$ LLVM.section_flags) @@
230231
fun (name,addr,size,off) (_,(r,w,x)) ->
231-
let addr = Int64.(addr + bias) in [
232+
let addr = Int64.(addr + bias) in
233+
provide_if Int64.(size <> 0L) [
232234
Ogre.provide segment addr size r w x >>= fun () ->
233235
Ogre.provide mapped addr size off >>= fun () ->
234236
Ogre.provide named_region addr size name
@@ -262,7 +264,7 @@ let provide_elf_segments =
262264
$ LLVM.elf_program_header_flags) @@
263265
fun (name,off,size) (_, addr, vsize) (_,ld,r,w,x) ->
264266
let addr = Int64.(addr + bias) in
265-
provide_if ld [
267+
provide_if (ld && Int64.(size <> 0L)) [
266268
Ogre.provide segment addr vsize r w x;
267269
Ogre.provide mapped addr size off;
268270
Ogre.provide named_region addr vsize name;
@@ -282,14 +284,15 @@ let provide_coff_segmentation = [
282284
$ LLVM.coff_virtual_section_header
283285
$ LLVM.section_flags) @@
284286
fun (name, _, size, start) (_,addr,vsize) (_,(r,w,x)) ->
285-
let addr = Int64.(addr + bias) in [
287+
let addr = Int64.(addr + bias) in
288+
provide_if Int64.(size <> 0L) [
286289
Ogre.provide segment addr vsize r w x;
287290
Ogre.provide mapped addr size start;
288291
Ogre.provide section addr vsize;
289292
Ogre.provide named_region addr vsize name;
290293
] @ provide_if x [
291-
Ogre.provide code_region addr vsize start;
292-
]
294+
Ogre.provide code_region addr vsize start;
295+
]
293296

294297
]
295298

plugins/primus_loader/primus_loader_basic.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ module Make(Param : Param)(Machine : Primus.Machine.S) = struct
8787
| Ok segs ->
8888
Machine.Seq.fold ~init:null segs
8989
~f:(fun endp {Image.Scheme.addr; size; info=(_,w,x)} ->
90+
assert Int64.(size <> 0L);
9091
make_word addr >>= fun lower ->
9192
make_word Int64.(size-1L) >>= fun diff ->
9293
let upper = Word.(lower + diff) in

0 commit comments

Comments
 (0)