Merge "Remove unused property"
diff --git a/cmd/symbol_inject/macho.go b/cmd/symbol_inject/macho.go
index 4a3ecc7..478e6de 100644
--- a/cmd/symbol_inject/macho.go
+++ b/cmd/symbol_inject/macho.go
@@ -38,37 +38,33 @@
 		return symbols[i].Value < symbols[j].Value
 	})
 
-	for i, symbol := range symbols {
-		if symbol.Sect == 0 {
-			continue
-		}
-		if symbol.Name == symbolName {
-			var nextSymbol *macho.Symbol
-			if i+1 < len(symbols) {
-				nextSymbol = &symbols[i+1]
+	for _, symbol := range symbols {
+		if symbol.Name == symbolName && symbol.Sect != 0 {
+			// Find the next symbol in the same section with a higher address
+			n := sort.Search(len(symbols), func(i int) bool {
+				return symbols[i].Sect == symbol.Sect &&
+					symbols[i].Value > symbol.Value
+			})
+
+			section := machoFile.Sections[symbol.Sect-1]
+
+			var end uint64
+			if n < len(symbols) {
+				end = symbols[n].Value
+			} else {
+				end = section.Addr + section.Size
 			}
-			return calculateMachoSymbolOffset(machoFile, symbol, nextSymbol)
+
+			if end <= symbol.Value && end > symbol.Value+4096 {
+				return maxUint64, maxUint64, fmt.Errorf("symbol end address does not seem valid, %x:%x", symbol.Value, end)
+			}
+
+			size := end - symbol.Value - 1
+			offset := uint64(section.Offset) + (symbol.Value - section.Addr)
+
+			return offset, size, nil
 		}
 	}
 
 	return maxUint64, maxUint64, fmt.Errorf("symbol not found")
 }
-
-func calculateMachoSymbolOffset(file *macho.File, symbol macho.Symbol, nextSymbol *macho.Symbol) (uint64, uint64, error) {
-	section := file.Sections[symbol.Sect-1]
-
-	var end uint64
-	if nextSymbol != nil && nextSymbol.Sect != symbol.Sect {
-		nextSymbol = nil
-	}
-	if nextSymbol != nil {
-		end = nextSymbol.Value
-	} else {
-		end = section.Addr + section.Size
-	}
-
-	size := end - symbol.Value - 1
-	offset := uint64(section.Offset) + (symbol.Value - section.Addr)
-
-	return offset, size, nil
-}
diff --git a/cmd/symbol_inject/pe.go b/cmd/symbol_inject/pe.go
index dad530d..82fdff8 100644
--- a/cmd/symbol_inject/pe.go
+++ b/cmd/symbol_inject/pe.go
@@ -32,41 +32,41 @@
 		symbolName = "_" + symbolName
 	}
 
-	sort.Slice(peFile.Symbols, func(i, j int) bool {
-		if peFile.Symbols[i].SectionNumber != peFile.Symbols[j].SectionNumber {
-			return peFile.Symbols[i].SectionNumber < peFile.Symbols[j].SectionNumber
+	symbols := peFile.Symbols
+	sort.Slice(symbols, func(i, j int) bool {
+		if symbols[i].SectionNumber != symbols[j].SectionNumber {
+			return symbols[i].SectionNumber < symbols[j].SectionNumber
 		}
-		return peFile.Symbols[i].Value < peFile.Symbols[j].Value
+		return symbols[i].Value < symbols[j].Value
 	})
 
-	for i, symbol := range peFile.Symbols {
+	for _, symbol := range symbols {
 		if symbol.Name == symbolName {
-			var nextSymbol *pe.Symbol
-			if i+1 < len(peFile.Symbols) {
-				nextSymbol = peFile.Symbols[i+1]
+			// Find the next symbol (n the same section with a higher address
+			n := sort.Search(len(symbols), func(i int) bool {
+				return symbols[i].SectionNumber == symbol.SectionNumber &&
+					symbols[i].Value > symbol.Value
+			})
+
+			section := peFile.Sections[symbol.SectionNumber-1]
+
+			var end uint32
+			if n < len(symbols) {
+				end = symbols[n].Value
+			} else {
+				end = section.Size
 			}
-			return calculatePESymbolOffset(peFile, symbol, nextSymbol)
+
+			if end <= symbol.Value && end > symbol.Value+4096 {
+				return maxUint64, maxUint64, fmt.Errorf("symbol end address does not seem valid, %x:%x", symbol.Value, end)
+			}
+
+			size := end - symbol.Value - 1
+			offset := section.Offset + symbol.Value
+
+			return uint64(offset), uint64(size), nil
 		}
 	}
 
 	return maxUint64, maxUint64, fmt.Errorf("symbol not found")
 }
-
-func calculatePESymbolOffset(file *pe.File, symbol *pe.Symbol, nextSymbol *pe.Symbol) (uint64, uint64, error) {
-	section := file.Sections[symbol.SectionNumber-1]
-
-	var end uint32
-	if nextSymbol != nil && nextSymbol.SectionNumber != symbol.SectionNumber {
-		nextSymbol = nil
-	}
-	if nextSymbol != nil {
-		end = nextSymbol.Value
-	} else {
-		end = section.Size
-	}
-
-	size := end - symbol.Value - 1
-	offset := section.Offset + symbol.Value
-
-	return uint64(offset), uint64(size), nil
-}
diff --git a/java/droiddoc.go b/java/droiddoc.go
index ac6bd33..82beb96 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -249,7 +249,7 @@
 	j.srcFiles = srcFiles.FilterOutByExt(".srcjar")
 
 	j.docZip = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"docs.zip")
-	j.stubsJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.jar")
+	j.stubsJar = android.PathForModuleOut(ctx, ctx.ModuleName()+"-"+"stubs.srcjar")
 
 	if j.properties.Local_sourcepaths == nil {
 		j.properties.Local_sourcepaths = append(j.properties.Local_sourcepaths, ".")
diff --git a/java/java_test.go b/java/java_test.go
index eda229b..8bae019 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -886,8 +886,8 @@
 		}
 		`)
 
-	stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.jar")
-	barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.jar")
+	stubsJar := filepath.Join(buildDir, ".intermediates", "bar-doc", "android_common", "bar-doc-stubs.srcjar")
+	barDoc := ctx.ModuleForTests("bar-doc", "android_common").Output("bar-doc-stubs.srcjar")
 	if stubsJar != barDoc.Output.String() {
 		t.Errorf("expected stubs Jar [%q], got %q", stubsJar, barDoc.Output.String())
 	}