This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Add support to load object files from thin archives
ClosedPublic

Authored by PRESIDENT810 on May 26 2022, 7:00 AM.

Details

Summary

This fixes https://github.com/llvm/llvm-project/issues/50114 where lldb/mac can't load object files from thin archives.
This patch allows lldb to identify thin archives, and load object files contained in them.

I'm a novice at llvm and I'm not sure whether I'm fixing this right, so please tell me if anything wrong with this fix, thank you guys!

Diff Detail

Event Timeline

PRESIDENT810 created this revision.May 26 2022, 7:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 26 2022, 7:00 AM
PRESIDENT810 requested review of this revision.May 26 2022, 7:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 26 2022, 7:00 AM
PRESIDENT810 edited the summary of this revision. (Show Details)May 26 2022, 7:03 AM
clayborg requested changes to this revision.May 26 2022, 11:33 AM

A few things to fix but nothing serious. Nice patch! We also need a test for this. I would add a test to lldb/test/API/functionalities/archives/TestBSDArchives.py

lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
279–280
304

No need to make a copy of this data, just make a string ref to it:

309
431

Add "archive_type" as a constructor argument for ObjectContainerBSDArchive instead of setting this afterward.

455

This shouldn't be needed right? If we already had the container cached, it should have the archive type set correctly already.

466–468

Now that we are checking two things, we can clean up this code with an early return if "armag" is null:

478–480
This revision now requires changes to proceed.May 26 2022, 11:33 AM

I have refactored my code so it should looks cleaner now, but I'm not sure how to add a test. It seems that adding a test for thin archive on macOS platforms can be not so straightforward. I see that in lldb/test/API/functionalities/archives/Makefile, the test suite is using libtool instead of ar to create archives on macOS platforms, and I don't know whether I can produce a thin archive with that. Also, ld on macOS platforms seems to be unable to identify thin archives (I'm using ld64.lld when testing my code locally).

I would really appreciate it if someone can provide some advice about how to implement such a test case here, thank you!

I have refactored my code so it should looks cleaner now, but I'm not sure how to add a test. It seems that adding a test for thin archive on macOS platforms can be not so straightforward. I see that in lldb/test/API/functionalities/archives/Makefile, the test suite is using libtool instead of ar to create archives on macOS platforms, and I don't know whether I can produce a thin archive with that. Also, ld on macOS platforms seems to be unable to identify thin archives (I'm using ld64.lld when testing my code locally).

I would really appreciate it if someone can provide some advice about how to implement such a test case here, thank you!

Does LLVM have the ability to generate a "llvm-ar" tool that can do the packaging? If so, an easy test would be to create a thin archive and then run the following API from within python:

static SBModuleSpecList SBModuleSpecList::GetModuleSpecifications(const char *path);

If you run this API on a thin archive without your modifications, then you will probably get an empty SBModuleSpecList object. So the flow would be:

  • make sure "llvm-ar" is built if it already isn't with the "check-lldb" target (might need to add it as a depedency
  • use the build "llvm-ar" to make an archive that points to two .o files that you build from sources
  • then run some python in your test case like:
# Make the Makefile that uses the built "llvm-ar"  one two source files that have .o created for them, and then it runs "llvm-ar" and will produce the "thin.a" in the output directory

# This line will point to the "thin.a" that was just built
archive_path = self.getBuildArtifact("thin.a")

# Get the module specs from the thin archive
module_specs = lldb.SBModuleSpecList.GetModuleSpecifications(archive_path)
num_specs = module_specs.GetSize()
self.assertEqual(num_specs, 2)

# For extra credit you can verify that you get the right file path from each file spec
obj_path_1 = self.getBuildArtifact("foo.o")
obj_path_2 = self.getBuildArtifact("bar.o")
self.assertEqual(module_specs.GetSpecAtIndex(0).GetObjectName(), obj_path_1)
self.assertEqual(module_specs.GetSpecAtIndex(1).GetObjectName(), obj_path_2)

Add tests by introducing llvm-ar to produce thin archives

clayborg accepted this revision.Jun 15 2022, 1:01 AM

Sorry for the delay, looks good with the test

This revision is now accepted and ready to land.Jun 15 2022, 1:01 AM

Hi, sorry to interrupt but I just forgot to mention that I didn't have access to commit. Anyone can please help me land this one if it looks ok?

thakis added a subscriber: thakis.Jul 4 2022, 12:39 PM

What --author flag should I use for you when landing this?

What --author flag should I use for you when landing this?

Hi! Could you please use --author="Kaining Zhong <zhongkaining.paxos@bytedance.com>"? Thanks!

This revision was automatically updated to reflect the committed changes.
thakis added a comment.Jul 5 2022, 2:06 AM

Done.

Thanks for the fix! :)