Skip to content

Commit 0df2712

Browse files
committed
Replace C2C.atom_is_extern by more robust criterion C2C.atom_is_external
This predicate is used in macOS and Cygwin ports to determine when a global symbol may be defined in a shared library, and therefore must be referenced through the GOT. The previous criterion was just "is it declared `extern`"? However, this misses some cases, e.g. "common" declaration. The new criterion is "it is not declared `static` and not defined in the current compilation unit", which should be a lot more robust. Fixes: #537
1 parent 044cfbc commit 0df2712

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

aarch64/extractionMachdep.v

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Extract Constant Archi.abi =>
2929

3030
Extract Constant SelectOp.symbol_is_relocatable =>
3131
"match Configuration.system with
32-
| ""macos"" -> C2C.atom_is_extern
32+
| ""macos"" -> C2C.atom_is_external
3333
| _ -> (fun _ -> false)".
3434

3535
(* Asm *)

cfrontend/C2C.ml

+12-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type inline_status =
3434

3535
type atom_info =
3636
{ a_storage: C.storage; (* storage class *)
37+
a_defined: bool; (* defined in the current comp. unit? *)
3738
a_size: int64 option; (* size in bytes *)
3839
a_alignment: int option; (* alignment *)
3940
a_sections: Sections.section_name list; (* in which section to put it *)
@@ -51,11 +52,13 @@ let atom_is_static a =
5152
with Not_found ->
5253
false
5354

54-
let atom_is_extern a =
55-
try
56-
(Hashtbl.find decl_atom a).a_storage = C.Storage_extern
57-
with Not_found ->
58-
false
55+
(* Is it possible for symbol [a] to be defined in a DLL? *)
56+
let atom_is_external a =
57+
match Hashtbl.find decl_atom a with
58+
| { a_defined = true } -> false
59+
| { a_storage = C.Storage_static } -> false
60+
| _ -> true
61+
| exception Not_found -> true
5962

6063
let atom_alignof a =
6164
try
@@ -588,6 +591,7 @@ let name_for_string_literal s =
588591
let mergeable = if is_C_string s then 1 else 0 in
589592
Hashtbl.add decl_atom id
590593
{ a_storage = C.Storage_static;
594+
a_defined = true;
591595
a_alignment = Some 1;
592596
a_size = Some (Int64.of_int (String.length s + 1));
593597
a_sections = [Sections.for_stringlit mergeable];
@@ -623,6 +627,7 @@ let name_for_wide_string_literal s ik =
623627
let mergeable = if is_C_wide_string s then wchar_size else 0 in
624628
Hashtbl.add decl_atom id
625629
{ a_storage = C.Storage_static;
630+
a_defined = true;
626631
a_alignment = Some wchar_size;
627632
a_size = Some (Int64.(mul (of_int (List.length s + 1))
628633
(of_int wchar_size)));
@@ -1156,6 +1161,7 @@ let convertFundef loc env fd =
11561161
Debug.atom_global fd.fd_name id';
11571162
Hashtbl.add decl_atom id'
11581163
{ a_storage = fd.fd_storage;
1164+
a_defined = true;
11591165
a_alignment = None;
11601166
a_size = None;
11611167
a_sections = Sections.for_function env loc id' fd.fd_attrib;
@@ -1247,6 +1253,7 @@ let convertGlobvar loc env (sto, id, ty, optinit) =
12471253
error "'%s' has incomplete type" id.name;
12481254
Hashtbl.add decl_atom id'
12491255
{ a_storage = sto;
1256+
a_defined = optinit <> None;
12501257
a_alignment = Some (Z.to_int al);
12511258
a_size = Some (Z.to_int64 sz);
12521259
a_sections = [section];

x86/extractionMachdep.v

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ Extract Constant Archi.win64 =>
2929

3030
Extract Constant SelectOp.symbol_is_external =>
3131
"match Configuration.system with
32-
| ""macos"" -> C2C.atom_is_extern
33-
| ""cygwin"" when Archi.ptr64 -> C2C.atom_is_extern
32+
| ""macos"" -> C2C.atom_is_external
33+
| ""cygwin"" when Archi.ptr64 -> C2C.atom_is_external
3434
| _ -> (fun _ -> false)".

0 commit comments

Comments
 (0)