aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cfrontend/C2C.ml4
-rw-r--r--common/Sections.ml29
-rw-r--r--common/Sections.mli4
3 files changed, 25 insertions, 12 deletions
diff --git a/cfrontend/C2C.ml b/cfrontend/C2C.ml
index 293e79f0..7f796fe3 100644
--- a/cfrontend/C2C.ml
+++ b/cfrontend/C2C.ml
@@ -1234,7 +1234,7 @@ let convertFundef loc env fd =
{ a_storage = fd.fd_storage;
a_alignment = None;
a_size = None;
- a_sections = Sections.for_function env id' fd.fd_attrib;
+ a_sections = Sections.for_function env loc id' fd.fd_attrib;
a_access = Sections.Access_default;
a_inline = inline;
a_loc = loc };
@@ -1311,7 +1311,7 @@ let convertGlobvar loc env (sto, id, ty, optinit) =
| Some i ->
convertInitializer env ty i in
let (section, access) =
- Sections.for_variable env id' ty (optinit <> None) in
+ Sections.for_variable env loc id' ty (optinit <> None) in
if Z.gt sz (Z.of_uint64 0xFFFF_FFFFL) then
error "'%s' is too big (%s bytes)"
id.name (Z.to_string sz);
diff --git a/common/Sections.ml b/common/Sections.ml
index 30be9e69..839128a5 100644
--- a/common/Sections.ml
+++ b/common/Sections.ml
@@ -160,9 +160,22 @@ let gcc_section name readonly exec =
sec_writable = not readonly; sec_executable = exec;
sec_access = Access_default }
+(* Check and extract whether a section was given as attribute *)
+
+let get_attr_section loc attr =
+ match Cutil.find_custom_attributes ["section"; "__section__"] attr with
+ | [] -> None
+ | [[C.AString name]] -> Some name
+ | [[_]] ->
+ Diagnostics.error loc "'section' attribute requires a string";
+ None
+ | _ ->
+ Diagnostics.error loc "ambiguous 'section' attribute";
+ None
+
(* Determine section for a variable definition *)
-let for_variable env id ty init =
+let for_variable env loc id ty init =
let attr = Cutil.attributes_of_type env ty in
let readonly = List.mem C.AConst attr && not(List.mem C.AVolatile attr) in
let si =
@@ -170,11 +183,11 @@ let for_variable env id ty init =
(* 1- Section explicitly associated with #use_section *)
Hashtbl.find use_section_table id
with Not_found ->
- match Cutil.find_custom_attributes ["section"; "__section__"] attr with
- | [[C.AString name]] ->
+ match get_attr_section loc attr with
+ | Some name ->
(* 2- Section given as an attribute, gcc-style *)
gcc_section name readonly false
- | _ ->
+ | None ->
(* 3- Default section appropriate for size and const-ness *)
let size =
match Cutil.sizeof env ty with Some sz -> sz | None -> max_int in
@@ -190,17 +203,17 @@ let for_variable env id ty init =
(* Determine sections for a function definition *)
-let for_function env id attr =
+let for_function env loc id attr =
let si_code =
try
(* 1- Section explicitly associated with #use_section *)
Hashtbl.find use_section_table id
with Not_found ->
- match Cutil.find_custom_attributes ["section"; "__section__"] attr with
- | [[C.AString name]] ->
+ match get_attr_section loc attr with
+ | Some name ->
(* 2- Section given as an attribute, gcc-style *)
gcc_section name true true
- | _ ->
+ | None ->
(* 3- Default section *)
try
Hashtbl.find current_section_table "CODE"
diff --git a/common/Sections.mli b/common/Sections.mli
index bc97814d..d9fd9239 100644
--- a/common/Sections.mli
+++ b/common/Sections.mli
@@ -46,7 +46,7 @@ val define_section:
-> ?writable:bool -> ?executable:bool -> ?access:access_mode -> unit -> unit
val use_section_for: AST.ident -> string -> bool
-val for_variable: Env.t -> AST.ident -> C.typ -> bool ->
+val for_variable: Env.t -> C.location -> AST.ident -> C.typ -> bool ->
section_name * access_mode
-val for_function: Env.t -> AST.ident -> C.attributes -> section_name list
+val for_function: Env.t -> C.location -> AST.ident -> C.attributes -> section_name list
val for_stringlit: unit -> section_name