How to get list and size of all imported symbol by dlsym() function in swift 5?

I want to list all symbol imported by the dlsym() and there respective size .

Code i am using for importing symbol in swift from .dylib(c binary) file

let dynammicFileHandle = dlopen("lib.dylib",RTLD_LOCAL|RTLD_NOW)
    if(dynammicFileHandle != nil){
        let sym = dlsym(dynammicFileHandle, "create")
        if(sym != nil){


Question :
Q1. How can i list all symbol imported by the dlsym() function ?
Q2. How can i get size of each imported symbol ?

dlsym doesn't import symbols. All the external symbols are imported when you dlopen() the file, based on your invocation. dlsym() only searches for the symbol identified by the string ("create" in your case) in the imported symbol tables and returns its starting address. If you were using RTLD_LAZY, dlsym() would link in the symbol if it found it in the external file.

I don't think there is anything in the dlopen() interface that will do what you want to do. You will probably have to build in the functionality into the library itself.

You would probably have to use a external tool like otool to look at the various sections and calculate sizes from address.

@jonprescott thanks !

@jonprescott for exporting my function from swift library (.dylib) do i need to add some statement like export before function defination ?
For example

//export swiftforum()
func swiftforum(){
 print("Hello swift forum")

As far as I know, for macOS and Linux, BSD, etc., you don't have to explicitly say that it is to be exported. Symbols are automatically external unless they are hidden, or made invisible.

If your library is only going to be used by Swift programs, you should be fine. If your library will be used in a mixed language environment, you might want to look through the forums about @convention( c ) to ensure that non-Swift code can reference the symbol.

Windows is a different story, and, since I don't program with Windows anymore, I'm not sure how the swift Windows folks handle the visibility of external symbols in dynamic libraries (dlls) which is different than macOS, et al.

@jonprescott thanks ! . problem solved by adding @_silgen_name("functionName") before function defination .
After creating swift library (.dylib) when i was trying to call swift function from swift library i was getting error cannot unsafeBitCast different two different sized data type.

type alias i have created .
typealias newUplink = (()-> (Int64,NSString))

method that i am using for calling swift function is mentioned below in the link .Only difference is that i am using swift .dylib file ?

You must be calling unsafeBitCast to cast between two instances of types with different sizes. Doesn't matter how you get the reference to the function you want to call. You might want to publish a public domain version of what you are trying to do to get some pointers. It's not clear from your question how to help you without more details.

Terms of Service

Privacy Policy

Cookie Policy