コード例 #1
0
        public SimpleRelocationSection(ISection section, SymbolTable <uint> symbols)
        {
            Section = section;

            var count = section.GetContents().Length / Relocation.SIZE;

            Relocations = new Relocation[count];

            for (int i = 0, start = 0; i < count; i++, start += Relocation.SIZE)
            {
                Relocations[i] = new Relocation(section.GetContents(), start, symbols);
            }
        }
コード例 #2
0
ファイル: ELFUtility.cs プロジェクト: toshipiazza/binskim
        /// <summary>
        /// Get the compilers used to create an ELF binary.
        /// </summary>
        /// <param name="elf">ELF binary</param>
        /// <returns>List of compiler tools from the .comment section</returns>
        internal static ELFCompiler[] GetELFCompilers(IELF elf)
        {
            ISection commentSection = elf.Sections.Where(s => s.Name == ".comment").FirstOrDefault();

            if (commentSection != null)
            {
                try
                {
                    string[] commentData = NullTermAsciiToStrings(commentSection.GetContents());
                    var      compilers   = new ELFCompiler[commentData.Length];
                    for (int i = 0; i < commentData.Length; i++)
                    {
                        compilers[i] = new ELFCompiler(commentData[i]);
                    }
                    return(compilers);
                }
                // Catch cases when the .comment section is not formatted the way we expect it to be.
                catch (Exception ex) when(ex is ArgumentException || ex is ArgumentNullException)
                {
                    return(new ELFCompiler[] { new ELFCompiler(string.Empty) });
                }
            }
            else
            {
                return(new ELFCompiler[] { new ELFCompiler(string.Empty) });
            }
        }
コード例 #3
0
        protected byte[] GetData(ISection section, ulong size, ulong offset)
        {
            Log.Debug($"AnELF.GetData: section == {section.Name}; size == {size}; offset == {offset:X}");
            byte[] data = section.GetContents();

            Log.Debug($"  data length: {data.Length} (long: {data.LongLength})");
            Log.Debug($"  offset: {offset}; size: {size}");
            if ((ulong)data.LongLength < (offset + size))
            {
                return(EmptyArray);
            }

            if (size == 0)
            {
                size = (ulong)data.Length - offset;
            }

            var ret = new byte[size];

            checked {
                Array.Copy(data, (int)offset, ret, 0, (int)size);
            }

            return(ret);
        }
コード例 #4
0
        private async Task ProcessFile(
            string file,
            bool hasUnwindingInfo,
            bool hasDwarfDebugInfo,
            ISection buildId,
            IELF elf)
        {
            _logger.LogInformation("Contains unwinding info: {hasUnwindingInfo}", hasUnwindingInfo);
            _logger.LogInformation("Contains DWARF debug info: {hasDwarfDebugInfo}", hasDwarfDebugInfo);

            var builder = new StringBuilder();
            var bytes   = buildId.GetContents().Skip(16);

            foreach (var @byte in bytes)
            {
                builder.Append(@byte.ToString("x2"));
            }

            var buildIdHex = builder.ToString();

            if (!(await _client.SendAsync(new HttpRequestMessage(HttpMethod.Head, _backendUrl)
            {
                Headers = { { "debug-id", buildIdHex } }
            }))
                .IsSuccessStatusCode)
            {
                return;
            }

            // Better would be if `ELF` class would expose its buffer so we don't need to read the file twice.
            // Ideally ELF would read headers as a stream which we could reset to 0 after reading heads
            // and ensuring it's what we need.
            using (var fileStream = File.OpenRead(file))
            {
                var postResult = await _client.SendAsync(new HttpRequestMessage(HttpMethod.Post, _backendUrl)
                {
                    Headers = { { "debug-id", buildIdHex } },
                    Content = new MultipartFormDataContent(
                        // TODO: add a proper boundary
                        $"Upload----WebKitFormBoundary7MA4YWxkTrZu0gW--")
                    {
                        { new StreamContent(fileStream), file }
                    }
                });

                if (!postResult.IsSuccessStatusCode)
                {
                    _logger.LogError("{statusCode} for file {file}", postResult.StatusCode, file);
                    if (postResult.Headers.TryGetValues("X-Error-Code", out var code))
                    {
                        _logger.LogError("Code: {code}");
                    }
                }
                else
                {
                    _logger.LogInformation("Sent file: {file}");
                }
            }

#if DEBUG
            _logger.LogInformation("Build Id: {buildIdHex}", buildIdHex);
            _logger.LogInformation("Class: {class}.", elf.Class);
            _logger.LogInformation("HasSectionsStringTable: {hasSectionsStringTable}.", elf.HasSectionsStringTable);
            _logger.LogInformation("HasSectionHeader: {hasSectionHeader}.", elf.HasSectionHeader);
            foreach (var section in elf.Sections)
            {
                _logger.LogInformation("Section: {section}.", section);
            }

            _logger.LogInformation("HasSegmentHeader: {hasSegmentHeader}.", elf.HasSegmentHeader);
            foreach (var segment in elf.Segments)
            {
                _logger.LogInformation("Segment: {segment}.", segment);
            }

            _logger.LogInformation("Endianness: {Eedianness}.", elf.Endianess);
            _logger.LogInformation("Machine: {machine}.", elf.Machine);
            _logger.LogInformation("Type: {type}.", elf.Type);
#endif
        }