Skip to content

Commit

Permalink
lib: fix error on MacOS
Browse files Browse the repository at this point in the history
Sections use a different syntax for Mach-O executables.

Fixes:

lib/bfd.c:35:1: error: argument to 'section' attribute is not valid for this target: mach-o section specifier requires a segment and section separated by a
      comma
DEFINE_MTYPE_STATIC(LIB, BFD_INFO, "BFD info")
^
./lib/memory.h:140:2: note: expanded from macro 'DEFINE_MTYPE_STATIC'
        DEFINE_MTYPE_ATTR(group, name, static, desc)                           \
        ^
./lib/memory.h:110:26: note: expanded from macro 'DEFINE_MTYPE_ATTR'
                __attribute__((section(".data.mtypes"))) = { {                 \
                                       ^
1 error generated.

Signed-off-by: Ruben Kerkhof <ruben@rubenkerkhof.com>
  • Loading branch information
rubenk committed Mar 19, 2020
1 parent 795fbef commit ad26e09
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions lib/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,20 @@ struct memgroup {
* but MGROUP_* aren't.
*/

#ifdef __MACH__
#define ATTRIBUTE_MGROUPS __attribute__((section("__DATA,mgroups")))
#else
#define ATTRIBUTE_MGROUPS __attribute__((section(".data.mgroups")))
#endif

#define DECLARE_MGROUP(name) extern struct memgroup _mg_##name;
#define DEFINE_MGROUP(mname, desc) \
struct memgroup _mg_##mname \
__attribute__((section(".data.mgroups"))) = { \
.name = desc, \
.types = NULL, \
.next = NULL, \
.insert = NULL, \
.ref = NULL, \
struct memgroup _mg_##mname ATTRIBUTE_MGROUPS = { \
.name = desc, \
.types = NULL, \
.next = NULL, \
.insert = NULL, \
.ref = NULL, \
}; \
static void _mginit_##mname(void) __attribute__((_CONSTRUCTOR(1000))); \
static void _mginit_##mname(void) \
Expand All @@ -101,27 +106,33 @@ struct memgroup {
*_mg_##mname.ref = _mg_##mname.next; \
}


#define DECLARE_MTYPE(name) \
extern struct memtype MTYPE_##name[1]; \
/* end */

#ifdef __MACH__
#define ATTRIBUTE_MTYPES __attribute__((section("__DATA,mtypes")))
#else
#define ATTRIBUTE_MTYPES __attribute__((section(".data.mtypes")))
#endif

#define DEFINE_MTYPE_ATTR(group, mname, attr, desc) \
attr struct memtype MTYPE_##mname[1] \
__attribute__((section(".data.mtypes"))) = { { \
.name = desc, \
.next = NULL, \
.n_alloc = 0, \
.size = 0, \
.ref = NULL, \
} }; \
attr struct memtype MTYPE_##mname[1] ATTRIBUTE_MTYPES = {{ \
.name = desc, \
.next = NULL, \
.n_alloc = 0, \
.size = 0, \
.ref = NULL, \
}}; \
static void _mtinit_##mname(void) __attribute__((_CONSTRUCTOR(1001))); \
static void _mtinit_##mname(void) \
{ \
if (_mg_##group.insert == NULL) \
_mg_##group.insert = &_mg_##group.types; \
MTYPE_##mname->ref = _mg_##group.insert; \
*_mg_##group.insert = MTYPE_##mname; \
_mg_##group.insert = &MTYPE_##mname->next; \
_mg_##group.insert = &MTYPE_##mname->next; \
} \
static void _mtfini_##mname(void) __attribute__((_DESTRUCTOR(1001))); \
static void _mtfini_##mname(void) \
Expand Down

0 comments on commit ad26e09

Please sign in to comment.