Skip to content

objdump -tT on a release built shared object library does not produce any symbols #24800

@srb623

Description

@srb623

Zig Version

0.15.0-dev.1466+3fb86841c

Steps to Reproduce and Observed Behavior

I have the following build.zig file to create a debug and release build of a simple shared object library that I am using to learn zig:

`const std = @import("std");

pub fn build(b: *std.Build) void
{
//
//The prefix for the shared object library.
//
const lib_prefix = "zigstring";

//
//The source file upon which the library depends.
//
const src_file = "src/string.zig";


//
//The target information for the build.
//
const target = b.standardTargetOptions(.{});


//
//The debug version of the library.
//
const libstring_debug = b.addLibrary
(
    .{
        .name = lib_prefix ++ "-debug",
        .linkage = .dynamic,
        .root_module = b.createModule
        (
            .{
                .optimize =  .Debug,
                .pic = true,
                .root_source_file = b.path(src_file),
                .target = target,
            }
        ),
    }
);


//
//The release version of the library.
//
const libstring_release = b.addLibrary
(
    .{
        .name = lib_prefix ++ "-release",
        .linkage = .dynamic,
        .root_module = b.createModule
        (
            .{
                .optimize =  .ReleaseSmall,
                .pic = true,
                .root_source_file = b.path(src_file),
                // .strip = true,
                .target = target,
            }
        ),
    }
);


//
//Now that we have defined the debug version of the library, we now must
//install it.
//
b.installArtifact
(
    libstring_debug,
);


//
//Now that we have defined the release version of the library, we now must
//install it.
//
b.installArtifact
(
    libstring_release
);

}
`

and a few functions in string.zig to test the above build file:

`//
//First we must import the standard library.
//
const std = @import("std");

//
//Now we need to define the type for strings so that we can use this throughout
//the code and also outside of this module.
//
pub const String_Data_Type = u8;

pub const String = [] const String_Data_Type;

//
//Now we need to define the allocator that we will use for the string functions.
//
const gpa = std.heap.GeneralPurposeAllocator
(
.{
.safety = true
}
);

//
//Routine to return true if the string FULL_STRING contains one or more
//instances of the string SEARCH_STRING and false otherwise.
//
pub fn contains
(
full_string : String,
search_string : String
) bool
{
//
//First we must ensure that the search string contains some data and we must
//terminate if this is not the case.
//
std.debug.assert(search_string.len != 0);

//
//Now that we know that we have valid parameters, we can now check to see if
//the search string is found at least once in the full string and return the
//flag indicating this.
//
return std.mem.containsAtLeast
(
    String_Data_Type,
    full_string,
    1,
    search_string
);

}

//
//Routine to return true if the string FULL_STRING contains one or more of
//the characters contained in the set of characters CHAR_SET and false
//otherwise.
//
pub fn contains_chars
(
full_string : String,
char_set : String
) bool
{
//
//First we must ensure that the character set contains some data and we must
//terminate if this is not the case.
//
std.debug.assert(full_string.len != 0);

//
//Now we need to loop for all of the characters in the character set and for
//each of them we determine if the character is in the full string and if so
//we can return true to the caller.
//
for (char_set) | char |
{
    if (std.mem.indexOfScalar
    (
        String_Data_Type,
        full_string,
        char
    ) != null)
    {
        //
        //The character CHAR was found in the full string and thus we can
        //return true to the caller.
        //
        return true;
    }
}

//
//Finally if we reach this point then none of the characters in CHAR_SET was
//found in the full string and thus we must return false to the caller.
//
return false;

}

//
//Routine to return true if the string FULL_STRING ends with the string
//SUFFIX and false
//otherwise.
//
pub fn ends_with
(
full_string : String,
suffix : String
) bool
{
//
//First we must ensure that the suffix contains some data and we terminate
//if this is not the case.
//
std.debug.assert(suffix.len != 0);

//
//Now we need to determine if the string FULL_STRING ends with SUFFIX and we
//return TRUE if this is the case and FALSE otherwise.
//
return std.mem.endsWith
(
    String_Data_Type,
    full_string,
    suffix
);

}
`

and I build it using the following command:

zig build \ --cache-dir /home/xxxxxxxx/.cache/zig \ --prefix build

When I do a "objdump -tT build/lib/libzigstring-release.so" I find the following output occurs:

build/lib/libzigstring-release.so: file format elf64-x86-64

SYMBOL TABLE:
no symbols

DYNAMIC SYMBOL TABLE:
no symbols

and I expected to see the public symbols contains, contains_chars and ends_with. I note that if I change "pub fn" to "export fn" then it will produce the required symbols and the size of the shared object file increases, presumably as object code is being output to the library.

Does this mean that the only type of shared object that you can create in zig must use the C ABI ? I was hoping to be able to create a zig shared object instead.

I also note that the documentation for the build system is out of date as 'addSharedLibrary' appears to have been removed in favour of 'addLibrary' and maybe my limited understanding of things (only started coding zig a few hours ago) is the problem.

Expected Behavior

Should see that the three public functions, contains, contains_chars and ends_with appear in the symbol list and that object code is being output to the library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions