Skip to content

Commit

Permalink
Support building on macOS/arm64
Browse files Browse the repository at this point in the history
On Arm-based Macs, -no_pie is ignored and gives a linker warning.
Moreover, the build falls over with:

  ld: Absolute addressing not allowed in arm64 code but used in '_image_type_ptr_aisimage' referencing '_image_type_aisimage'

for dumpimage and mkimage, since we put data structs in text sections
not data sections and so cannot have dynamic relocations. Instead, move
the sections to __DATA and drop disabling PIE.

Signed-off-by: Jessica Clarke <jrtc27@jrtc27.com>
Reviewed-by: Tom Rini <trini@konsulko.com>
  • Loading branch information
jrtc27 authored and trini committed Mar 28, 2021
1 parent 6863c7f commit 3b14204
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 7 deletions.
5 changes: 0 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,6 @@ HOSTCC = $(call os_x_before, 10, 5, "cc", "gcc")
KBUILD_HOSTCFLAGS += $(call os_x_before, 10, 4, "-traditional-cpp")
KBUILD_HOSTLDFLAGS += $(call os_x_before, 10, 5, "-multiply_defined suppress")

# since Lion (10.7) ASLR is on by default, but we use linker generated lists
# in some host tools which is a problem then ... so disable ASLR for these
# tools
KBUILD_HOSTLDFLAGS += $(call os_x_before, 10, 7, "", "-Xlinker -no_pie")

# macOS Mojave (10.14.X)
# Undefined symbols for architecture x86_64: "_PyArg_ParseTuple"
KBUILD_HOSTLDFLAGS += $(call os_x_after, 10, 14, "-lpython -dynamclib", "")
Expand Down
4 changes: 2 additions & 2 deletions tools/imagetool.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,14 @@ int rockchip_copy_image(int fd, struct image_tool_params *mparams);

#define INIT_SECTION(name) do { \
unsigned long name ## _len; \
char *__cat(pstart_, name) = getsectdata("__TEXT", \
char *__cat(pstart_, name) = getsectdata("__DATA", \
#name, &__cat(name, _len)); \
char *__cat(pstop_, name) = __cat(pstart_, name) + \
__cat(name, _len); \
__cat(__start_, name) = (void *)__cat(pstart_, name); \
__cat(__stop_, name) = (void *)__cat(pstop_, name); \
} while (0)
#define SECTION(name) __attribute__((section("__TEXT, " #name)))
#define SECTION(name) __attribute__((section("__DATA, " #name)))

struct image_type_params **__start_image_type, **__stop_image_type;
#else
Expand Down

14 comments on commit 3b14204

@k-ronny
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi
I am trying to build uboot in an OpenWrt build environment on a macOS x86-64 build system.
With this commit I get a segmentation fault 11 when running the mkimage tool.
After some searching I got the idea that getsectdata may not be the right approach but getsectiondata.
If I use the attached patch mkimage does the right thing on my build system.
Please can you help me with this?
Thank you very much.

--- a/tools/imagetool.h	2021-04-22 18:33:54.000000000 +0200
+++ b/tools/imagetool.h	2021-04-22 18:37:08.000000000 +0200
@@ -270,13 +270,15 @@
  *  b) we need a API call to get the respective section symbols */
 #if defined(__MACH__)
 #include <mach-o/getsect.h>
+#include <mach-o/ldsyms.h>
 
 #define INIT_SECTION(name)  do {					\
 		unsigned long name ## _len;				\
-		char *__cat(pstart_, name) = getsectdata("__DATA",	\
+		unsigned char *__cat(pstart_, name) = getsectiondata(	\
+			&_mh_execute_header, "__DATA",			\
 			#name, &__cat(name, _len));			\
-		char *__cat(pstop_, name) = __cat(pstart_, name) +	\
-			__cat(name, _len);				\
+		unsigned char *__cat(pstop_, name) = 			\
+			__cat(pstart_, name) + __cat(name, _len);	\
 		__cat(__start_, name) = (void *)__cat(pstart_, name);	\
 		__cat(__stop_, name) = (void *)__cat(pstop_, name);	\
 	} while (0)

@k-ronny
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi
please can someone take a look at this? @jrtc27 @trini
With macOS version 11.4-x86_64 I still get a segmentation fault 11 running the mkimage tool.
getsectiondata is undocumented but we can adjust the address by calling _dyld_get_image_vmaddr_slide(uint32_t image_index) and
image_index 0 here means main executable.

--- a/tools/imagetool.h	2021-06-16 14:57:45.000000000 +0200
+++ b/tools/imagetool.h	2021-06-16 15:00:57.000000000 +0200
@@ -270,11 +270,14 @@
  *  b) we need a API call to get the respective section symbols */
 #if defined(__MACH__)
 #include <mach-o/getsect.h>
+#include <mach-o/dyld.h>
 
 #define INIT_SECTION(name)  do {					\
 		unsigned long name ## _len;				\
 		char *__cat(pstart_, name) = getsectdata("__DATA",	\
 			#name, &__cat(name, _len));			\
+			__cat(pstart_, name) +=				\
+				_dyld_get_image_vmaddr_slide(0);	\
 		char *__cat(pstop_, name) = __cat(pstart_, name) +	\
 			__cat(name, _len);				\
 		__cat(__start_, name) = (void *)__cat(pstart_, name);	\

@jrtc27
Copy link
Contributor Author

@jrtc27 jrtc27 commented on 3b14204 Jun 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@k-ronny
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but is that really necessary in this case?

@ptpt52
Copy link

@ptpt52 ptpt52 commented on 3b14204 Dec 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@k-ronny hi, your patch fix the issue for me, but just some changes base of:

diff --git a/tools/imagetool.h b/tools/imagetool.h
index f055649..da317e4 100644
--- a/tools/imagetool.h
+++ b/tools/imagetool.h
@@ -271,11 +271,15 @@ int rockchip_copy_image(int fd, struct image_tool_params *mparams);
  *  b) we need a API call to get the respective section symbols */
 #if defined(__MACH__)
 #include <mach-o/getsect.h>
+#include <mach-o/dyld.h>

-#define INIT_SECTION(name)  do {                                       \
+#define INIT_SECTION(name)     struct image_type_params                \
+       **__cat(__start_, name), **__cat(__stop_, name);                \
+       do {                                                            \
                unsigned long name ## _len;                             \
                char *__cat(pstart_, name) = getsectdata("__DATA",      \
-                       #name, &__cat(name, _len));                     \
+                       #name, &__cat(name, _len)) +                    \
+                       _dyld_get_image_vmaddr_slide(0);                \
                char *__cat(pstop_, name) = __cat(pstart_, name) +      \
                        __cat(name, _len);                              \
                __cat(__start_, name) = (void *)__cat(pstart_, name);   \
@@ -283,7 +287,6 @@ int rockchip_copy_image(int fd, struct image_tool_params *mparams);
        } while (0)
 #define SECTION(name)   __attribute__((section("__DATA, " #name)))

-struct image_type_params **__start_image_type, **__stop_image_type;
 #else
 #define INIT_SECTION(name) /* no-op for ELF */
 #define SECTION(name)   __attribute__((section(#name)))

@k-ronny
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrtc27 Hi,
in the meantime there was a PR (https://lists.denx.de/pipermail/u-boot/2021-December/468875.html) to address this problem. May I ask you to take note of this / a look at it? There are at least two projects (of which I know) that have to deal with this problem: Homebrew and OpenWrt.
Thank you.

@jrtc27
Copy link
Contributor Author

@jrtc27 jrtc27 commented on 3b14204 Jan 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please bounce that email to me so I can easily reply?

@k-ronny
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR is from @svlobanov so we should ask him.

@svlobanov Could you do that?

@svlobanov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please bounce that email to me so I can easily reply?

I've sent to jrtc27@the-same-domain dot com

@svlobanov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrtc27 could you please take a look at 2nd version https://lists.denx.de/pipermail/u-boot/2022-January/472133.html ?

@jrtc27
Copy link
Contributor Author

@jrtc27 jrtc27 commented on 3b14204 Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still not subscribed to the mailing list and don't intend to, can you please just bounce it to me again? (Ideally would've just been Cc'ed on that original email...)

@trini
Copy link
Contributor

@trini trini commented on 3b14204 Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the record, U-Boot is also on lore.kernel.org so b4/lei work as well as the links to download as mbox, etc, there. And thanks folks!

@jrtc27
Copy link
Contributor Author

@jrtc27 jrtc27 commented on 3b14204 Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I didn't realise that, thanks, downloading from lore is easy

@svlobanov
Copy link
Contributor

@svlobanov svlobanov commented on 3b14204 Jan 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrtc27 I have already added you to the thread in maillist

Please sign in to comment.