Reputation: 372
I am trying to develop a library for Mac OS for scientific computation. It works and compiles file on Linux, but for some reason I am getting weird errors on Mac.
ld: __DATA_CONST segment missing SG_READ_ONLY flag in '/Users/ec2-user/libdescriptor/bin/libdescriptor.dylib'
or
ld: __DATA_CONST segment permissions is not 'rw-' in '/Users/ec2-user/libdescriptor/bin/libdescriptor.dylib'
It is a python pybind11 module linked against my library.
Below is the make file for my library, 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
It compiles the code, then invokes a custom lto plugin called Enzyme to differentiate the functions. Therefore it uses LLVM 13, and lld linker instead of default Mac OS linker.
This compiles without errors. When I link it against my Pybind11 module (CMake file below), I get the _DATA_CONST
errors I mentioned above. I have tried swapping linkers and compilers on the pybind11 step, but no help.
Cmakefile for pybind11 module:
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})
What is this error, and how to approach it? below is the otools output if that is helpful
(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
Upvotes: 1
Views: 232