How to exclude a module from symbol parsing of lldb? - lldb

Are there any lldb settings via I can exclude a loaded module from the initial lldb symbol parsing?

Not really.
You can create your target with target create -dtrue and lldb won't search for any dependent libraries. But when you run and libraries start loading, lldb will do the initial parse as libraries load.
You can set the setting target.preload-symbols to false and lldb will try to only look for symbol information "when it needs it". This isn't limited to a specific library, but if you are careful to specify the target shared libraries for your breakpoints you can reduce the "whole world" searches that cause lldb to have to read in symbols.
If the problem is that some libraries take too long to parse, you might try the new symbols.enable-lldb-index-cache setting, which caches the results of the symbol parsing for use in future lldb sessions.

Related

At dynamic linking, does the dynamic loader look at all object files for definitions, or only at those specified by the executable?

So I'm trying to wrap my head around static and dynamic linking. There are many resources on SO and on the web. I think I pretty much get it, but there's still one thing that seems to bother me. Also, please correct me if my overall understanding is wrong.
I think I understand static linking:
The linker unpacks the linked libraries, and actually includes the libraries' object files inside the produced executable. The unresolved-stubs in the application object files are then replaced by actual function-calling code, which calls functions in addresses known at build time.
Dynamic linking on the other hand is what puzzles me more: I understand that in dynamic linking, the stubs in the object-code which reference yet-unresolved names, are going to stay as stubs until runtime.
Then at runtime, the dynamic loader of the OS would look through precompiled libraries stored at standard filesystem locations. It would look in the object-files of the libraries, inside their symbol tables (?) and try to find a matching function definition for each unresolved-stub. It would then load the matching object-files into memory, and replace the stubs to point to the function definitions.
So the part I'm missing is this: where does the OS dynamic loader look - does it look in the symbol tables for all object-files in the system-libraries directory? Or does it only look in object-files specified somewhere in the application-executable file? Is this the reason why at compile time we must specify all dynamic dependencies of our program? Also, is it true dynamic libraries expose a symbol-table too?
So the part I'm missing is this: where does the OS dynamic loader look
- does it look in the symbol tables for all object-files in the system-libraries directory?
No dynamic linker I'm aware of does this.
Or does it only look in object-files
specified somewhere in the application-executable file?
Nor exactly this, either.
Details vary, but generally, a dynamic linker looks for specific shared libraries by name in various directories. The directories searched may be built into the linker, specified by the operating system, specified in the object being linked, or a combination. The linker does not (generally) examine libraries' symbol tables until after it locates them by name and selects them for linking.
Is this the
reason why at compile time we must specify all dynamic dependencies of
our program?
Yes, though under some circumstances we do not need to specify all dynamic dependencies at compile time. Some dynamic linkers support on-demand dynamic loading as directed by the program itself. This can be used to implement plugin systems, among other purposes.
Also, is it true dynamic libraries expose a symbol-table
too?
Yes. Dynamic libraries have their own symbol tables because
The dynamic linker uses them to do its work, and
Dynamic libraries can have their own dynamic linking requirements, which are not necessarily reflected in the main program's.
In the normal usage, "dynamic linking" is performed by the loader. "Static linking" is performed by the linker.
Generally, linkers can create either executable files or shared libraries. The linker output for both is an instruction stream that tells the loaders how to place the executable or library in memory.
Dynamic linking on the other hand is what puzzles me more: I understand that in dynamic linking, the stubs in the object-code which reference yet-unresolved names, are going to stay as stubs until runtime
That is not [usually] correct. The linker will locate the shared library in which the symbol exists. The executable will have an instruction to find the symbol in that shared library. Linkers generally puke if they cannot find all the symbols that need to be resolved.
So the part I'm missing is this: where does the OS dynamic loader look - does it look in the symbol tables for all object-files in the system-libraries directory?
This a system specific question. In well designed operating systems, the shared libraries are designated by the system manager. The loader uses the library specified by the system. Poorly designed systems frequently use some kind of search path to find the shared libraries (which created a massive security hole).

Resolve shared library path on Windows and *nix systems

When loading shared library given its name, systems searches for the actual file (eg .dll) in some directories, based on search order, or in cache.
How can I programmatically get the resolved path of DLL given its name, but without actually loading it? E.g. on Windows, for kernel32 or kernel32.dll it would probably return C:\windows\system32\kernel32.dll whereas given foo it could be C:\Program Files\my\app\foo.dll.
If that can't be done, is there another way to determinate whether certain library belongs to system? E.g. user32.dll or libc.so.6 are system libraries but avcodec-55.dll or myhelperslib.so are not.
I'm interested solutions that work on Windows, Linux and Mac OS.
On Windows, LoadLibraryEx has the LOAD_LIBRARY_AS_DATAFILE flag which opens the DLL without performing the operations you refer to as "actually loading it".
This can be combined with any of the search order flags (Yeah, there is more than just one search order).
Unfortunately, you cannot use GetModuleFilename. Use GetMappedFileName instead.
The LoadLibraryEx documentation also says specifically not to use the SearchPath function to locate DLLs and not to use the DONT_RESOLVE_DLL_REFERENCES flag mentioned in comments.
For Linux, there's an existing tool ldd for which source code is available. It does actually load the shared libraries, but with a special environment variable LD_TRACE_LOADED_OBJECTS set that by convention causes them to skip doing anything. Because this is just a convention, beware that malicious files can perform actions when loaded by ldd CVE-2009-5064.

How to programmatically look up a symbol in a running application

I have an application which I'm trying to debug, however running it under gdb is producing different results, and it would be nice to have it output true symbol information when confronted with an address. for instance.
I have a method which is called periodically and I can determine the address of the call site. However, I'd like to print out the symbol information at run time for this address. I know I can run "nm" on the executable but that is outside of the application. I want to be able to do it from within the application itself.
I'm using GCC 4.7.2 on a linux platform.
(eddited to explain why I can't use gdb)
Dynamic symbol information can be accessed via the DT_DYNAMIC segment, which is loaded into memory and can be accessed by asking dlopen(3) for a handle to the main executable.
Static symbol information can be read only from the actual executable file, or an external file, as it is not listed in the loadable segments.
With just dynamic information, you will not be able to resolve anything that is not exported, which means you will most likely only see library calls unless your executable has its symbol table exported, so static information is probably the way to go.
This involves either lots of parsing, or using the bfd library built from binutils.
I'd seriously wonder if that was really worth the effort, though. You might get the same information from using the profiling support in gcc.

undefined reference when calling code in downloadable kernel module

Following on from a previous question Using system symbol table from VxWorks RTP.
I am porting an existing vxworks kernel module to an RTP, however, much of the actual functionality is still embedded in other "downloadable" kernel modules. These are loaded using loadModule (as part of the startup process), and so should be in the system symbol table at the point my RTP runs.]
However, I cannot get my RTP to build, getting errors from the linker regarding undefined references.
How can I persuade the linker to ignore references to these functions? I am passing the -Xbind-lazy -non-static options to the linker.
As an example, the kernel module (built as database.out) might contain code such as
void writeDatabaseValue(int , char*);
and my RTP application code calls it in the usual way
void main (int)
{
//do some stuff
writeDatabaseValue(0,"foo"); //Linker error here
taskDelay(100); //but no linker error here
}
I have tried adding database.out to the link line using -l:database.out, which resolves the reference to writeDatabaseValue, however I then get into dependency hell, as the linker then tries to resolve all of database.outs references, including all of the standard vxworks api calls.
EDIT: Just for extra complication, some of the kernel module code I want to call is in the same module that actually spawned my RTP......
You are really trying to shoehorn one environment into a totally different one and are experiencing the pain inherent in this approach. I don't believe you will be able to get the linker to ignore the references.
Might be better to use symbolic links (available from within the Workbench environment) to source files and use those in a RTP project.
Or create dynamic libraries based on the DKMs (again, use symbolic links) and link those against your RTP.
As an example, let's say you have the following Workbench workspace:
DKM1
DKM2
RTP
Based on the question, you are attempting to link DKM1 & 2's object files in the RTP and are experiencing pain.
A better approach is to add a project link to the source files in DKM1 & DKM2 and have them compiled in the RTP. Because it is a link (instead of a copy) then changing the file in the DKM (to fix a bug for example) would also change it in the RTP.
In the RTP project, do a "New > Directory". Select the "advanced" button and check the "link to existing directory" then select the "DKM1" directory.
All the source file in the DKM1 will now be part of the RTP. Do the same with DKM2.
When you compile the RTP project, these new files should also be compiled.

How do I instruction ld to look for additional directories with c?

I'm asking the solution in c , NOT command line options.
The reason I'm sure it exists is that Apache can be instructed to loaded .so dynamically:
load modules/mod_perl.so
UPDATE
Isn't it too inefficient if I need to search with dlsym one by one?
If you want to load your own modules dynamically, study the dlopen/dlsym family of functions. That's what Apache uses to load its modules. man dlopen has all the information.
If you want to link against shared libraries, you must use linker command line options to specify where these libraries are. Otherwise your program will not be able to execute. No amount of C programming can help a program that won't even start.

Resources