Skip to contentSkip to conversation

Resolve link errors on Mac OS X

PATCH NAK Christopher Friedt 2 participants 4 messages May 3, 2016

lore
Share & tools
lore
#0 lore raw

cover Resolve link errors on Mac OS X

Hi list,

I recently tried to build Qemu on Mac and ran into a couple of trivial issues that I've provided patches for. I suppose that normally people just use 'brew install qemu', but there is really no reason that it can't be built from source, particularly for those modifying Qemu regularly.

In any case, the first change moves to using 'libtool -static' to create libraries on Mac OS X. If one attempts to use ar and ranlib, then the final link will fail with error messages resembling the following:

ld: warning: ignoring file libqemuutil.a, file was built for archive which is not the architecture being linked (x86_64): libqemuutil.a Undefined symbols for architecture x86_64:
...

Notice ld (Apple's ld64) presumes the static library is a relocatable with architecture 'archive' rather than x86_64, in this case.

A similar fix is required for dtc - again quite trivial, but I can provide that if necessary.

The second patch removes the preprocessor conditional around the function event_notifier_init_fd() in util/event_notifier-posix.c so that the link does not fail on systems where CONFIG_POSIX is defined but CONFIG_EVENTFD is not (such as under Mac OS X).

There is more information in each of the commits that follows.

Please feel free to comment.

Cheers,

C

Christopher Friedt (2):
Use libtool instead of ar to create static libraries on Darwin. Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy
link

 rules.mak                   | 4 ++++
 util/event_notifier-posix.c | 2 --
 2 files changed, 4 insertions(+), 2 deletions(-)
Signature · 1 line
2.6.4 (Apple Git-63)
#1 lore raw

PATCH 1/2 Use libtool instead of ar to create static libraries on Darwin.

Currently, at least on Mac OS X 10.11.4 (El Capitan), Qemu fails to build for a few reasons.

One of those reasons is that Apple's ld (at least ld64) does not properly process archive files created with ar (even Apple's ar).

After some RTFM'ing, I came upon this tidbit, which is unfortunate. Luckily, autotools packages are not broken.

"Libtool with -static is intended to replace ar(5) and ranlib."
http://www.manpages.info/macosx/libtool.1.html

In any case, this change takes Apple's recommendations into account and allows Qemu to build on Mac OS X El Capitan.

---
 rules.mak | 4 ++++
 1 file changed, 4 insertions(+)
rules.mak +4 -0
index d1ff311..44421af 100644
@@ -105,7 +105,11 @@ modules:
 	$(call LINK,$(filter %.o %.a %.mo, $^))
 
 %.a:
+ifeq ($(shell uname),Darwin)
+	$(call quiet-command,rm -f $@ && libtool -static -o $@ $^,"  libtool    $(TARGET_DIR)$@")
+else
 	$(call quiet-command,rm -f $@ && $(AR) rcs $@ $^,"  AR    $(TARGET_DIR)$@")
+endif
 
 quiet-command = $(if $(V),$1,$(if $(2),@echo $2 && $1, @$1))
 
Signature · 1 line
2.6.4 (Apple Git-63)
#2 lore raw

PATCH 2/2 Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy link

In reply to Christopher Friedt

The file ivshmem.c unconditionally references event_notifier_init_fd() in util/event_notifier-posix.c, even if CONFIG_EVENTFD is not defined. On platforms where CONFIG_POSIX is defined, but CONFIG_EVENTFD is not defined, that results in an undefined symbol referenced from ivshmem.c and the link fails. That applies to Mac OS X, but possibly other BSD-based distros.

Note: there is nothing specific to eventfd inside and event_notifier_init() also fails unconditionally if CONFIG_EVENTFD is not defined.

---
 util/event_notifier-posix.c | 2 --
 1 file changed, 2 deletions(-)
util/event_notifier-posix.c +0 -2
index c1f0d79..c9bb34d 100644
@@ -21,7 +21,6 @@
 #include <sys/eventfd.h>
 #endif
 
-#ifdef CONFIG_EVENTFD
 /*
  * Initialize @e with existing file descriptor @fd.
  * @fd must be a genuine eventfd object, emulation with pipe won't do.
@@ -31,7 +30,6 @@ void event_notifier_init_fd(EventNotifier *e, int fd)
     e->rfd = fd;
     e->wfd = fd;
 }
-#endif
 
 int event_notifier_init(EventNotifier *e, int active)
 {
Signature · 1 line
2.6.4 (Apple Git-63)
#3 lore raw

Re: [Qemu-devel] [PATCH 2/2] [RESENT-INLINE] Remove unnecessary CONFIG_EVENTFD preprocessor conditional to satisfy link

Quoted 1 line · Christopher Friedt

The file ivshmem.c unconditionally references event_notifier_init_fd() in util/event_notifier-posix.c, even if CONFIG_EVENTFD is not defined. On platforms where CONFIG_POSIX is defined, but CONFIG_EVENTFD is not defined, that results in an undefined symbol referenced from ivshmem.c and the link fails. That applies to Mac OS X, but possibly other BSD-based distros.

ivshmem.c cannot work without CONFIG_EVENTFD, and therefore is not compiled without it.

A link error for event_notifier_init_fd() from ivshmem.o indicates you got CONFIG_EVENTFD=y for make (since ivshmem.o gets linked), but !defined(CONFIG_EVENTFD) for C (or else event_notifier_init_fd() would exist). Your build tree is messed up, or the makefiles are broken. Try starting over with a fresh build tree.

See also
http://lists.nongnu.org/archive/html/qemu-devel/2016-04/msg01437.html

Quoted 1 line

Note: there is nothing specific to eventfd inside and event_notifier_init() also fails unconditionally if CONFIG_EVENTFD is not defined.

I'm afraid you're misreading the code.

event_notifier_init() has a working fallback: if the host doesn't support eventfd (!defined(CONFIG_EVENTFD) or eventfd() fails with ENOSYS), then we emulate eventfd with a pair of pipes. Possible only because pipes have special properties.

To support the emulation, we have two file descriptors, @rfd for reading and @wfd for writing. Design invariant: if they are the same file descriptor, it must be a proper eventfd, and if they are different, they must be a pair of pipes. The only way to establish the invariant with event_notifier_init_fd() is passing it an eventfd, as the function's contract says. Without CONFIG_EVENTFD, there is no way to use the function correctly, so defining it would be nothing but a trap.

See also commit 330b583, which added the ifdef.

Quoted 18 lines
---
 util/event_notifier-posix.c | 2 --
 1 file changed, 2 deletions(-)
util/event_notifier-posix.c +0 -1
index c1f0d79..c9bb34d 100644
@@ -21,7 +21,6 @@
 #include <sys/eventfd.h>
 #endif
 
-#ifdef CONFIG_EVENTFD
 /*
  * Initialize @e with existing file descriptor @fd.
  * @fd must be a genuine eventfd object, emulation with pipe won't do.

util/event_notifier-posix.c void event_notifier_init_fd(EventNotifier *e, int fd) · line 35

Quoted 7 lines
util/event_notifier-posix.c +0 -1
@@ -31,7 +30,6 @@ void event_notifier_init_fd(EventNotifier *e, int fd)
     e->rfd = fd;
     e->wfd = fd;
 }
-#endif
 
 int event_notifier_init(EventNotifier *e, int active)
 {

NAK