Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 1 | //===-- SBSection.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/API/SBSection.h" |
| 10 | #include "lldb/API/SBStream.h" |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 11 | #include "lldb/API/SBTarget.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Module.h" |
Greg Clayton | d9dc52d | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Section.h" |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 14 | #include "lldb/Symbol/ObjectFile.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/DataBuffer.h" |
| 16 | #include "lldb/Utility/DataExtractor.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/StreamString.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 19 | |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 20 | using namespace lldb; |
| 21 | using namespace lldb_private; |
| 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | SBSection::SBSection() : m_opaque_wp() {} |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | SBSection::SBSection(const SBSection &rhs) : m_opaque_wp(rhs.m_opaque_wp) {} |
| 26 | |
| 27 | SBSection::SBSection(const lldb::SectionSP §ion_sp) |
| 28 | : m_opaque_wp() // Don't init with section_sp otherwise this will throw if |
| 29 | // section_sp doesn't contain a valid Section * |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 30 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | if (section_sp) |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 32 | m_opaque_wp = section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 35 | const SBSection &SBSection::operator=(const SBSection &rhs) { |
| 36 | m_opaque_wp = rhs.m_opaque_wp; |
| 37 | return *this; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | SBSection::~SBSection() {} |
| 41 | |
| 42 | bool SBSection::IsValid() const { |
| 43 | SectionSP section_sp(GetSP()); |
| 44 | return section_sp && section_sp->GetModule().get() != NULL; |
| 45 | } |
| 46 | |
| 47 | const char *SBSection::GetName() { |
| 48 | SectionSP section_sp(GetSP()); |
| 49 | if (section_sp) |
| 50 | return section_sp->GetName().GetCString(); |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | lldb::SBSection SBSection::GetParent() { |
| 55 | lldb::SBSection sb_section; |
| 56 | SectionSP section_sp(GetSP()); |
| 57 | if (section_sp) { |
| 58 | SectionSP parent_section_sp(section_sp->GetParent()); |
| 59 | if (parent_section_sp) |
| 60 | sb_section.SetSP(parent_section_sp); |
| 61 | } |
| 62 | return sb_section; |
| 63 | } |
| 64 | |
| 65 | lldb::SBSection SBSection::FindSubSection(const char *sect_name) { |
| 66 | lldb::SBSection sb_section; |
| 67 | if (sect_name) { |
| 68 | SectionSP section_sp(GetSP()); |
| 69 | if (section_sp) { |
| 70 | ConstString const_sect_name(sect_name); |
| 71 | sb_section.SetSP( |
| 72 | section_sp->GetChildren().FindSectionByName(const_sect_name)); |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 73 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | } |
| 75 | return sb_section; |
Greg Clayton | fed39aa | 2012-06-27 22:22:28 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 78 | size_t SBSection::GetNumSubSections() { |
| 79 | SectionSP section_sp(GetSP()); |
| 80 | if (section_sp) |
| 81 | return section_sp->GetChildren().GetSize(); |
| 82 | return 0; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | lldb::SBSection SBSection::GetSubSectionAtIndex(size_t idx) { |
| 86 | lldb::SBSection sb_section; |
| 87 | SectionSP section_sp(GetSP()); |
| 88 | if (section_sp) |
| 89 | sb_section.SetSP(section_sp->GetChildren().GetSectionAtIndex(idx)); |
| 90 | return sb_section; |
| 91 | } |
| 92 | |
| 93 | lldb::SectionSP SBSection::GetSP() const { return m_opaque_wp.lock(); } |
| 94 | |
| 95 | void SBSection::SetSP(const lldb::SectionSP §ion_sp) { |
| 96 | m_opaque_wp = section_sp; |
| 97 | } |
| 98 | |
| 99 | lldb::addr_t SBSection::GetFileAddress() { |
| 100 | lldb::addr_t file_addr = LLDB_INVALID_ADDRESS; |
| 101 | SectionSP section_sp(GetSP()); |
| 102 | if (section_sp) |
| 103 | return section_sp->GetFileAddress(); |
| 104 | return file_addr; |
| 105 | } |
| 106 | |
| 107 | lldb::addr_t SBSection::GetLoadAddress(lldb::SBTarget &sb_target) { |
| 108 | TargetSP target_sp(sb_target.GetSP()); |
| 109 | if (target_sp) { |
| 110 | SectionSP section_sp(GetSP()); |
Greg Clayton | e72dfb3 | 2012-02-24 01:59:29 +0000 | [diff] [blame] | 111 | if (section_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | return section_sp->GetLoadBaseAddress(target_sp.get()); |
| 113 | } |
| 114 | return LLDB_INVALID_ADDRESS; |
| 115 | } |
| 116 | |
| 117 | lldb::addr_t SBSection::GetByteSize() { |
| 118 | SectionSP section_sp(GetSP()); |
| 119 | if (section_sp) |
| 120 | return section_sp->GetByteSize(); |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | uint64_t SBSection::GetFileOffset() { |
| 125 | SectionSP section_sp(GetSP()); |
| 126 | if (section_sp) { |
| 127 | ModuleSP module_sp(section_sp->GetModule()); |
| 128 | if (module_sp) { |
| 129 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 130 | if (objfile) |
| 131 | return objfile->GetFileOffset() + section_sp->GetFileOffset(); |
| 132 | } |
| 133 | } |
| 134 | return UINT64_MAX; |
| 135 | } |
| 136 | |
| 137 | uint64_t SBSection::GetFileByteSize() { |
| 138 | SectionSP section_sp(GetSP()); |
| 139 | if (section_sp) |
| 140 | return section_sp->GetFileSize(); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | SBData SBSection::GetSectionData() { return GetSectionData(0, UINT64_MAX); } |
| 145 | |
| 146 | SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) { |
| 147 | SBData sb_data; |
| 148 | SectionSP section_sp(GetSP()); |
| 149 | if (section_sp) { |
| 150 | const uint64_t sect_file_size = section_sp->GetFileSize(); |
| 151 | if (sect_file_size > 0) { |
| 152 | ModuleSP module_sp(section_sp->GetModule()); |
| 153 | if (module_sp) { |
| 154 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 155 | if (objfile) { |
| 156 | const uint64_t sect_file_offset = |
| 157 | objfile->GetFileOffset() + section_sp->GetFileOffset(); |
| 158 | const uint64_t file_offset = sect_file_offset + offset; |
| 159 | uint64_t file_size = size; |
| 160 | if (file_size == UINT64_MAX) { |
| 161 | file_size = section_sp->GetByteSize(); |
| 162 | if (file_size > offset) |
| 163 | file_size -= offset; |
| 164 | else |
| 165 | file_size = 0; |
| 166 | } |
Jonas Devlieghere | 87e403a | 2018-11-12 21:24:50 +0000 | [diff] [blame] | 167 | auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer( |
Zachary Turner | 7f6a7a3 | 2017-03-06 23:42:14 +0000 | [diff] [blame] | 168 | objfile->GetFileSpec().GetPath(), file_size, file_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 169 | if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) { |
| 170 | DataExtractorSP data_extractor_sp( |
| 171 | new DataExtractor(data_buffer_sp, objfile->GetByteOrder(), |
| 172 | objfile->GetAddressByteSize())); |
| 173 | |
| 174 | sb_data.SetOpaque(data_extractor_sp); |
| 175 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 176 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 178 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 179 | } |
| 180 | return sb_data; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | SectionType SBSection::GetSectionType() { |
| 184 | SectionSP section_sp(GetSP()); |
| 185 | if (section_sp.get()) |
| 186 | return section_sp->GetType(); |
| 187 | return eSectionTypeInvalid; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Abhishek Aggarwal | 3c7f070 | 2016-09-08 12:22:56 +0000 | [diff] [blame] | 190 | uint32_t |
| 191 | SBSection::GetPermissions() const |
| 192 | { |
| 193 | SectionSP section_sp(GetSP()); |
| 194 | if (section_sp) |
| 195 | return section_sp->GetPermissions(); |
| 196 | return 0; |
| 197 | } |
| 198 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 199 | uint32_t SBSection::GetTargetByteSize() { |
| 200 | SectionSP section_sp(GetSP()); |
| 201 | if (section_sp.get()) |
| 202 | return section_sp->GetTargetByteSize(); |
| 203 | return 0; |
Greg Clayton | d9dc52d | 2011-09-24 05:04:40 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | bool SBSection::operator==(const SBSection &rhs) { |
| 207 | SectionSP lhs_section_sp(GetSP()); |
| 208 | SectionSP rhs_section_sp(rhs.GetSP()); |
| 209 | if (lhs_section_sp && rhs_section_sp) |
| 210 | return lhs_section_sp == rhs_section_sp; |
| 211 | return false; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | bool SBSection::operator!=(const SBSection &rhs) { |
| 215 | SectionSP lhs_section_sp(GetSP()); |
| 216 | SectionSP rhs_section_sp(rhs.GetSP()); |
| 217 | return lhs_section_sp != rhs_section_sp; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | bool SBSection::GetDescription(SBStream &description) { |
| 221 | Stream &strm = description.ref(); |
| 222 | |
| 223 | SectionSP section_sp(GetSP()); |
| 224 | if (section_sp) { |
| 225 | const addr_t file_addr = section_sp->GetFileAddress(); |
| 226 | strm.Printf("[0x%16.16" PRIx64 "-0x%16.16" PRIx64 ") ", file_addr, |
| 227 | file_addr + section_sp->GetByteSize()); |
| 228 | section_sp->DumpName(&strm); |
| 229 | } else { |
| 230 | strm.PutCString("No value"); |
| 231 | } |
| 232 | |
| 233 | return true; |
Matthew Gardiner | c928de3 | 2014-10-22 07:22:56 +0000 | [diff] [blame] | 234 | } |