@@ -49,3 +49,184 @@ def test_unified_section_list(self):
4949 debug_info_section = main_exe_module .FindSection (".debug_info" )
5050 self .assertTrue (debug_info_section .IsValid ())
5151 self .assertGreater (debug_info_section .file_size , 0 )
52+
53+ def test_unified_section_list_overwrite_larger_section (self ):
54+ """
55+ Test the merging of an ELF file with another ELF File where all the new sections are bigger, validating we
56+ overwrite .comment from SHT_NOBITS to the new SHT_PROGBITS section and the smaller .text with the larger
57+ .text
58+ """
59+ exe = self .getBuildArtifact ("a.out" )
60+ self .yaml2obj ("main.yaml" , exe )
61+
62+ target = self .dbg .CreateTarget (exe )
63+ self .assertTrue (target , VALID_TARGET )
64+ main_exe_module = target .GetModuleAtIndex (0 )
65+
66+ # First we verify out .text section is the expected BEC0FFEE
67+ text_before_merge = main_exe_module .FindSection (".text" )
68+ self .assertTrue (text_before_merge .IsValid ())
69+ error = lldb .SBError ()
70+ section_content = text_before_merge .data .ReadRawData (
71+ error , 0 , text_before_merge .data .size
72+ )
73+ self .assertTrue (error .Success ())
74+ self .assertEqual (section_content , bytes .fromhex ("BEC0FFEE" ))
75+
76+ # .comment in main.yaml should be SHT_NOBITS, and size 0
77+ comment_before_merge = main_exe_module .FindSection (".comment" )
78+ self .assertTrue (comment_before_merge .IsValid ())
79+ self .assertEqual (comment_before_merge .data .size , 0 )
80+
81+ # yamlize the main.largertext.yaml and force symbol loading
82+ debug_info = self .getBuildArtifact ("a.out.debug" )
83+ self .yaml2obj ("main.largertext.yaml" , debug_info )
84+
85+ ci = self .dbg .GetCommandInterpreter ()
86+ res = lldb .SBCommandReturnObject ()
87+ ci .HandleCommand (f"target symbols add { debug_info } " , res )
88+ self .assertTrue (res .Succeeded ())
89+
90+ # verify we took the larger .text section
91+ main_exe_module_after_merge = target .GetModuleAtIndex (0 )
92+ text_after_merge = main_exe_module_after_merge .FindSection (".text" )
93+ self .assertTrue (text_after_merge .IsValid ())
94+ self .assertGreater (text_after_merge .data .size , text_before_merge .data .size )
95+ section_content_after_merge = text_after_merge .data .ReadRawData (
96+ error , 0 , text_after_merge .data .size
97+ )
98+ self .assertTrue (error .Success ())
99+ self .assertEqual (section_content_after_merge , bytes .fromhex ("BEC0FFEEEEFF0CEB" ))
100+
101+ # in main.largertext.yaml comment is not SHT_NOBITS, and so we should see
102+ # the size > 0 and equal to BAADF00D
103+ comment_after_merge = main_exe_module_after_merge .FindSection (".comment" )
104+ self .assertTrue (comment_after_merge .IsValid ())
105+ comment_content_after_merge = comment_after_merge .data .ReadRawData (
106+ error , 0 , comment_after_merge .data .size
107+ )
108+
109+ self .assertTrue (error .Success ())
110+ self .assertEqual (comment_content_after_merge , bytes .fromhex ("BAADF00D" ))
111+
112+ def test_unified_section_list_overwrite_smaller_section (self ):
113+ """
114+ Test the merging of an ELF file with another ELF File where all the existing sections are bigger, validating we don't
115+ overwrite with the SHT_NOBITS for .comment or the smaller .text section.
116+ """
117+ exe = self .getBuildArtifact ("a.out" )
118+ self .yaml2obj ("main.largertext.yaml" , exe )
119+
120+ target = self .dbg .CreateTarget (exe )
121+ self .assertTrue (target , VALID_TARGET )
122+ main_exe_module = target .GetModuleAtIndex (0 )
123+
124+ # Same as above test but inverse, verify our larger .text section
125+ # is the expected BEC0FFEE palindrome
126+ text_before_merge = main_exe_module .FindSection (".text" )
127+ self .assertTrue (text_before_merge .IsValid ())
128+ error = lldb .SBError ()
129+ section_content = text_before_merge .data .ReadRawData (
130+ error , 0 , text_before_merge .data .size
131+ )
132+ self .assertTrue (error .Success ())
133+ self .assertEqual (section_content , bytes .fromhex ("BEC0FFEEEEFF0CEB" ))
134+
135+ # Comment is SHT_PROGBITS on the larger yaml and should remain
136+ # the same after merge.
137+ comment_before_merge = main_exe_module .FindSection (".comment" )
138+ self .assertTrue (comment_before_merge .IsValid ())
139+ comment_content = comment_before_merge .data .ReadRawData (
140+ error , 0 , comment_before_merge .data .size
141+ )
142+
143+ self .assertTrue (error .Success ())
144+ self .assertEqual (comment_content , bytes .fromhex ("BAADF00D" ))
145+
146+ debug_info = self .getBuildArtifact ("a.out.debug" )
147+ self .yaml2obj ("main.yaml" , debug_info )
148+
149+ ci = self .dbg .GetCommandInterpreter ()
150+ res = lldb .SBCommandReturnObject ()
151+ ci .HandleCommand (f"target symbols add { debug_info } " , res )
152+ self .assertTrue (res .Succeeded ())
153+
154+ # Verify we didn't replace the sections after merge.s
155+ main_exe_module_after_merge = target .GetModuleAtIndex (0 )
156+ text_after_merge = main_exe_module_after_merge .FindSection (".text" )
157+ self .assertTrue (text_after_merge .IsValid ())
158+ self .assertEqual (text_after_merge .data .size , text_before_merge .data .size )
159+ section_content_after_merge = text_after_merge .data .ReadRawData (
160+ error , 0 , text_after_merge .data .size
161+ )
162+ self .assertTrue (error .Success ())
163+ self .assertEqual (section_content_after_merge , bytes .fromhex ("BEC0FFEEEEFF0CEB" ))
164+
165+ comment_after_merge = main_exe_module_after_merge .FindSection (".comment" )
166+ self .assertTrue (comment_after_merge .IsValid ())
167+ comment_content_after_merge = comment_after_merge .data .ReadRawData (
168+ error , 0 , comment_after_merge .data .size
169+ )
170+
171+ self .assertTrue (error .Success ())
172+ self .assertEqual (comment_content_after_merge , bytes .fromhex ("BAADF00D" ))
173+
174+ def test_unified_section_list_overwrite_mixed_merge (self ):
175+ """
176+ Test the merging of an ELF file with another ELF File where the lhs has a larger .comment section
177+ and the RHS has a larger .text section.
178+ """
179+ exe = self .getBuildArtifact ("a.out" )
180+ self .yaml2obj ("main.largercomment.yaml" , exe )
181+
182+ target = self .dbg .CreateTarget (exe )
183+ self .assertTrue (target , VALID_TARGET )
184+ main_exe_module = target .GetModuleAtIndex (0 )
185+
186+ # Verify we have the expected smaller BEC0FFEE
187+ text_before_merge = main_exe_module .FindSection (".text" )
188+ self .assertTrue (text_before_merge .IsValid ())
189+ error = lldb .SBError ()
190+ section_content = text_before_merge .data .ReadRawData (
191+ error , 0 , text_before_merge .data .size
192+ )
193+ self .assertTrue (error .Success ())
194+ self .assertEqual (section_content , bytes .fromhex ("BEC0FFEE" ))
195+
196+ # Verify we have the larger palindromic comment
197+ comment_before_merge = main_exe_module .FindSection (".comment" )
198+ self .assertTrue (comment_before_merge .IsValid ())
199+ comment_content = comment_before_merge .data .ReadRawData (
200+ error , 0 , comment_before_merge .data .size
201+ )
202+
203+ self .assertTrue (error .Success ())
204+ self .assertEqual (comment_content , bytes .fromhex ("BAADF00DF00DBAAD" ))
205+
206+ debug_info = self .getBuildArtifact ("a.out.debug" )
207+ self .yaml2obj ("main.largertext.yaml" , debug_info )
208+
209+ ci = self .dbg .GetCommandInterpreter ()
210+ res = lldb .SBCommandReturnObject ()
211+ ci .HandleCommand (f"target symbols add { debug_info } " , res )
212+ self .assertTrue (res .Succeeded ())
213+
214+ # Verify we replaced .text
215+ main_exe_module_after_merge = target .GetModuleAtIndex (0 )
216+ text_after_merge = main_exe_module_after_merge .FindSection (".text" )
217+ self .assertTrue (text_after_merge .IsValid ())
218+ section_content_after_merge = text_after_merge .data .ReadRawData (
219+ error , 0 , text_after_merge .data .size
220+ )
221+ self .assertTrue (error .Success ())
222+ self .assertEqual (section_content_after_merge , bytes .fromhex ("BEC0FFEEEEFF0CEB" ))
223+
224+ # Verify .comment is still the same.
225+ comment_after_merge = main_exe_module_after_merge .FindSection (".comment" )
226+ self .assertTrue (comment_after_merge .IsValid ())
227+ comment_content_after_merge = comment_after_merge .data .ReadRawData (
228+ error , 0 , comment_after_merge .data .size
229+ )
230+
231+ self .assertTrue (error .Success ())
232+ self .assertEqual (comment_content_after_merge , bytes .fromhex ("BAADF00DF00DBAAD" ))
0 commit comments