1 //===-- SBSection.cpp -------------------------------------------*- C++ -*-===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/API/SBSection.h" 10 #include "lldb/API/SBStream.h" 11 #include "lldb/API/SBTarget.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Core/Section.h" 14 #include "lldb/Symbol/ObjectFile.h" 15 #include "lldb/Utility/DataBuffer.h" 16 #include "lldb/Utility/DataExtractor.h" 17 #include "lldb/Utility/Log.h" 18 #include "lldb/Utility/StreamString.h" 19 20 using namespace lldb; 21 using namespace lldb_private; 22 23 SBSection::SBSection() : m_opaque_wp() {} 24 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 * 30 { 31 if (section_sp) 32 m_opaque_wp = section_sp; 33 } 34 35 const SBSection &SBSection::operator=(const SBSection &rhs) { 36 m_opaque_wp = rhs.m_opaque_wp; 37 return *this; 38 } 39 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)); 73 } 74 } 75 return sb_section; 76 } 77 78 size_t SBSection::GetNumSubSections() { 79 SectionSP section_sp(GetSP()); 80 if (section_sp) 81 return section_sp->GetChildren().GetSize(); 82 return 0; 83 } 84 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()); 111 if (section_sp) 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 } 167 auto data_buffer_sp = FileSystem::Instance().CreateDataBuffer( 168 objfile->GetFileSpec().GetPath(), file_size, file_offset); 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 } 176 } 177 } 178 } 179 } 180 return sb_data; 181 } 182 183 SectionType SBSection::GetSectionType() { 184 SectionSP section_sp(GetSP()); 185 if (section_sp.get()) 186 return section_sp->GetType(); 187 return eSectionTypeInvalid; 188 } 189 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 199 uint32_t SBSection::GetTargetByteSize() { 200 SectionSP section_sp(GetSP()); 201 if (section_sp.get()) 202 return section_sp->GetTargetByteSize(); 203 return 0; 204 } 205 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; 212 } 213 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; 218 } 219 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; 234 } 235