Comment 1 for bug 1664725

Revision history for this message
Donald A. Dade (donald-dade) wrote :

$ cat main.cpp
#include <efi.h>
#include <efilib.h>

EFI_STATUS
EFIAPI
efi_main (EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
   InitializeLib(ImageHandle, SystemTable);
   Print(L"Hello, world!\n");

   return EFI_SUCCESS;
}

$ cat Makefile
ARCH = $(shell uname -m | sed s,i[3456789]86,ia32,)

OBJS = main.o
TARGET = hello.efi

EFIINC = /usr/include/efi
EFIINCS = -I$(EFIINC) -I$(EFIINC)/$(ARCH) -I$(EFIINC)/protocol
LIB = /usr/lib
EFILIB = /usr/lib
EFI_CRT_OBJS = $(EFILIB)/crt0-efi-$(ARCH).o
EFI_LDS = $(EFILIB)/elf_$(ARCH)_efi.lds

CXXFLAGS = $(EFIINCS) -fno-stack-protector -fpic \
                  -fshort-wchar -mno-red-zone -Wall
ifeq ($(ARCH),x86_64)
  CFLAGS += -DEFI_FUNCTION_WRAPPER
endif

LDFLAGS = -nostdlib -znocombreloc -T $(EFI_LDS) -shared \
                  -Bsymbolic -L $(EFILIB) -L $(LIB) $(EFI_CRT_OBJS)

all: $(TARGET)

hello.so: $(OBJS)
        ld $(LDFLAGS) $(OBJS) -o $@ -lefi -lgnuefi

%.efi: %.so
        objcopy -j .text -j .sdata -j .data -j .dynamic \
                -j .dynsym -j .rel -j .rela -j .reloc \
                --target=efi-app-$(ARCH) $^ $@

clean :
        rm -rf $(TARGET) $(OBJS)

realclean: clean

$ make
ld -nostdlib -znocombreloc -T /usr/lib/elf_x86_64_efi.lds -shared -Bsymbolic -L /usr/lib -L /usr/lib /usr/lib/crt0-efi-x86_64.o main.o -o hello.so -lefi -lgnuefi
ld: /usr/lib/crt0-efi-x86_64.o: relocation R_X86_64_PC32 against undefined symbol `efi_main' can not be used when making a shared object; recompile with -fPIC
ld: final link failed: Bad value
Makefile:25: recipe for target 'hello.so' failed
make: *** [hello.so] Error 1