Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ELF module not loading PublicSymbols for function name resolution #45

Open
dedmen opened this issue Oct 14, 2021 · 0 comments
Open

ELF module not loading PublicSymbols for function name resolution #45

dedmen opened this issue Oct 14, 2021 · 0 comments

Comments

@dedmen
Copy link

dedmen commented Oct 14, 2021

When you load a ELF module that doesn't have Dwarf debug information, but does have exported public functions
(Like for example libpthread that you load when trying to analyze a core dump)
These functions are not considered when trying to look up a function name by instruction pointer.

But it works perfectly fine if you consider them.

I know this is not the proper way to do it, but I want to note it here in case someone who can do it properly comes along.

Add

        public PublicSymbol(string name, ulong address, ELFSharp.ELF.Sections.SymbolType type, ulong size)
        {
            Name = name;
            Address = address;
            demangledName = SimpleCache.Create(() => Demangle(name));
            Type = type;
            Size = size;
        }

        public ELFSharp.ELF.Sections.SymbolType Type { get; private set; }

        public ulong Size { get; private set; }

here https://github.com/southpolenator/SharpDebug/blob/next/Source/SharpDebug.DwarfSymbolProvider/IDwarfImage.cs#L39

and

publicSymbols.Add(new PublicSymbol(symbol.Name, symbol.Value - CodeSegmentOffset,symbol.Type, symbol.Size));

here: https://github.com/southpolenator/SharpDebug/blob/next/Source/SharpDebug.DwarfSymbolProvider/ElfImage.cs#L60

and (this is the most hacky part)

 functionsCache.AddRange(
                            publicSymbols
                                .Where(x => x.Type == SymbolType.Function && x.Size != 0 && x.Address != 0)
                                .Where(x =>
                                    functionsCache.All(f =>
                                        f.GetConstantAttribute(DwarfAttribute.LowPc) != x.Address) // #TODO binary search
                            ).Select(publicSymbol =>
                            {

                                Dictionary<DwarfAttribute, DwarfAttributeValue> attributes = new Dictionary<DwarfAttribute, DwarfAttributeValue>();
                                attributes.Add(DwarfAttribute.Name, new DwarfAttributeValue() { Type = DwarfAttributeValueType.String, Value = publicSymbol.Name });
                                // LowPC is function start
                                attributes.Add(DwarfAttribute.LowPc, new DwarfAttributeValue() { Type = DwarfAttributeValueType.Address, Value = publicSymbol.Address });
                                // HighPc can either be address (end address) or constant (offset after start address, function size)
                                attributes.Add(DwarfAttribute.HighPc, new DwarfAttributeValue() { Type = DwarfAttributeValueType.Constant, Value = publicSymbol.Size });
                                attributes.Add(DwarfAttribute.ByteSize, new DwarfAttributeValue() { Type = DwarfAttributeValueType.Constant, Value = publicSymbol.Size });
                                attributes.Add(DwarfAttribute.Type, new DwarfAttributeValue() { Type = DwarfAttributeValueType.Constant, Value = publicSymbol.Size });

                                return new DwarfSymbol { Tag = DwarfTag.Subprogram, Attributes = attributes };
                            })
                        );

here: https://github.com/southpolenator/SharpDebug/blob/next/Source/SharpDebug.DwarfSymbolProvider/DwarfSymbolProviderModule.cs#L1697

After these changes you will be able to resolve function names in libraries without debugging info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant