`__DATA_CONST` 在 Mac OS M1 上将库链接到 pybind11 模块时出错

问题描述 投票:0回答:1

我正在尝试为 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 的自定义 lto 插件来区分函数。因此它使用 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
macos clang apple-m1 lld
1个回答
0
投票

转到目标->构建设置“ENABLE_DEBUG_DYLIB”设置为否

© www.soinside.com 2019 - 2024. All rights reserved.