我正在尝试开发一个用于科学计算的 Mac OS 库。它可以在 Linux 上运行并编译文件,但出于某种原因,我在 Mac.ld 上遇到了奇怪的错误:__DATA_CONST 段丢失
我正在尝试开发一个用于 Mac OS 的科学计算库。它在 Linux 上可以运行并编译文件,但出于某种原因,我在 Mac 上遇到了奇怪的错误。
ld: __DATA_CONST segment missing SG_READ_ONLY flag in '/Users/ec2-user/libdescriptor/bin/libdescriptor.dylib'
或者
ld: __DATA_CONST segment permissions is not 'rw-' in '/Users/ec2-user/libdescriptor/bin/libdescriptor.dylib'
它是一个与我的库链接的 python pybind11 模块。
以下是我的库的 make 文件, libdescriptor.dylib
CXX := /opt/homebrew/Cellar/llvm@13/13.0.1_2/bin/clang++
CXXFLAGS := -std=c++17 -O3 -dynamiclib -fuse-ld=lld -flto -Iinclude
#Enzyme lib location
ENZYME_LIB := /usr/local/lib/LLDEnzyme-13.dylib
# Directories
SRCDIR := src
INCDIR := include
OBJDIR := obj
BINDIR := bin
# Output library
TARGET := $(BINDIR)/libdescriptor.dylib
# Source files
SRCFILES := $(wildcard $(SRCDIR)/*.cpp) $(wildcard $(SRCDIR)/*/*.cpp)
# Object files
OBJFILES := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SRCFILES))
# Create directories if they don't exist
$(shell mkdir -p $(BINDIR) $(OBJDIR) $(OBJDIR)/maths)
# Default target
all: $(TARGET)
# Link the executable
$(TARGET): $(OBJFILES)
$(CXX) -flto $(CXXFLAGS) -o $@ $^ -Wl,--lto-legacy-pass-manager -Wl,-mllvm -load=$(ENZYME_LIB) -Wl,-mllvm,-enzyme-loose-types
# -Wl,-segprot,__DATA_CONST,r,r <- modifications suggested by ChatGPT
#-Wl,-segprot,__DATA_CONST,ro,ro
# Compile source files into object files
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
# Clean up build files
clean:
rm -rf $(OBJDIR) $(BINDIR)
.PHONY: all clean
它编译代码,然后调用名为 Enzyme 来区分函数。因此,它使用 LLVM 13 和 lld 链接器,而不是默认的 Mac OS 链接器。
编译时没有错误。当我将其链接到我的 Pybind11 模块(下面的 CMake 文件)时,我得到了 _DATA_CONST
上面提到的错误。我尝试在 pybind11 步骤上交换链接器和编译器,但没有帮助。
pybind11 模块的 Cmakefile:
cmake_minimum_required(VERSION 3.16)
project(libdescriptor)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
# Ensure using LLVM's LLD linker
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld -Wl,-segprot,__DATA_CONST,r,r")
# Include directories
include_directories(include)
# Pybind11
find_package(pybind11 REQUIRED)
# Source files for the Pybind11 module
file(GLOB PYSOURCES python_bindings/*.cpp python_bindings/**/*.cpp)
# Create the Pybind11 module
pybind11_add_module(libdescriptor ${PYSOURCES})
# Path to your custom library
set(MY_CUSTOM_LIB_PATH /Users/ec2-user/libdescriptor/bin)
find_library(DESCRIPTOR_LIB NAMES descriptor PATHS ${MY_CUSTOM_LIB_PATH})
if(NOT DESCRIPTOR_LIB)
message(FATAL_ERROR "libdescriptor.dylib not found in ${MY_CUSTOM_LIB_PATH}")
endif()
# Link the Pybind11 module against the custom library
target_link_libraries(libdescriptor PUBLIC ${DESCRIPTOR_LIB})
target_include_directories(libdescriptor PUBLIC ${MY_CUSTOM_LIB_PATH})
这是什么错误,如何处理?以下是 otools 的输出,如果有帮助的话
(python3) ec2-user@ip-172-31-22-4 bin % otool -l ../bin/libdescriptor.dylib | grep -A 20 '__DATA_CONST'
segname __DATA_CONST
vmaddr 0x0000000000064000
vmsize 0x0000000000004000
fileoff 409600
filesize 16384
maxprot 0x00000003
initprot 0x00000003
nsects 2
flags 0x0
Section
sectname __got
segname __DATA_CONST
addr 0x0000000000064000
size 0x0000000000000110
offset 409600
align 2^3 (8)
reloff 0
nreloc 0
flags 0x00000006
reserved1 0 (index into indirect symbol table)
reserved2 0
Section
sectname __const
segname __DATA_CONST
addr 0x0000000000064110
size 0x0000000000000a30
offset 409872
align 2^3 (8)
reloff 0
nreloc 0
flags 0x00000000
reserved1 0
reserved2 0
Load command 2
cmd LC_SEGMENT_64
cmdsize 312
segname __DATA
vmaddr 0x0000000000068000
vmsize 0x0000000000064000
fileoff 425984
filesize 327680
maxprot 0x00000003
initprot 0x00000003
nsects 3